Click here to Skip to main content
15,899,634 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
------------------------------
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.
------------------------------

C#
// 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?
------------------------------
C#
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;
}


What I have tried:

in all sites of internet.in all sites of internet.
Posted
Updated 2-Sep-19 4:18am
v4

C# is a strongly typed language, and it required defintion of all variables before you can use them. It's also case sensitive, so t is not the same as T:
private static void MyFunction_Alternative<t>(dynamic MyVariable)
{   System.Type Origen_Type = (T != null) ? T : MyVariable.GetType();
 
Share this answer
 
It is a translator error. It is actually <T>.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900