Standard function with/without <T> parameter

  • Thread starter Thread starter zequion1
  • Start date Start date
Z

zequion1

Guest
------------------------------
Question 1:
I am working with system library functions that must be standard for the occasions when I have generic parameters of type <t> and those for occasions when I do not have <t> but I have a variable.

I find that, for each function I perform, I have to do two initializing functions that end up passing a System.Type parameter to the final function that standardizes.

In order not to need to do this, I would need something like what appears in "MyFunction_Alternative". but:
- It is not valid: System.Type Source_Type = (T! = Null)? T: MyVariable.GetType ();
- It is not valid: <t?>.

I would like to see <t> Nulable, because it would solve the problem.
------------------------------

// Function to Employ when I have a Variable but I don't have <t>.
public static void Function_NoT_Ini(dynamic MyVariable)
{ System.Type Origen_Type = MyVariable.GetType();
MyFunction_(Origen_Type);
}

// Function to Employ when I have <t>.
public static void Function_T_Ini<t>()
{ System.Type Origen_Type = typeof(T);
MyFunction_(Origen_Type);
}

// Standard Final Function.
private static void MyFunction_(System.Type Origen_Type)
{ System.Console.WriteLine("The values of the Enum are:");
foreach (int MyEnum_Value in System.Enum.GetValues(Origen_Type))
{ System.Console.WriteLine(MyEnum_Value);
}
}


// Alternative Final Function. It's not valid. It is also not valid <t?>.
private static void MyFunction_Alternative<t>(dynamic MyVariable)
{ System.Type Origen_Type = (T != null) ? T : MyVariable.GetType();

System.Console.WriteLine("The values of the Enum are:");
foreach (int MyEnum_Value in System.Enum.GetValues(Origen_Type))
{ System.Console.WriteLine(MyEnum_Value);
}
}

------------------------------
Question 2:
I have the problem of functions in which I need to do Cast when I don't have <t>.
Is it the right system?
------------------------------
public static void MyFunction_Cast<t>(dynamic MyVariable)
{ dynamic MyValue = 10;

// Cast with <t>.
T MyCast_Variable = (T)MyValue;

// Cast without <t>.
System.Type Origen_Type = MyVariable.GetType();

// It is not valid.
dynamic My_Second_Variable_of_MyVariable_Type = (Origen_Type)MyValue;

// It is valid, but is it the right system?
dynamic My_Second_Variable_of_MyVariable_Type = System.Activator.CreateInstance(Origen_Type);

// For example, if the type is an enumeration, it would convert the value 10 to Enumeration.MyValue10 (Not sure)
My_Second_Variable_of_MyVariable_Type_ = MyValue;
}

Continue reading...
 
Back
Top