none
Standard function with/without <T> parameter RRS feed

  • Question

  • ------------------------------
    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_ = System.Convert.ChangeType(MyVariable, Origen_Type );
    }



    • Edited by zequion1 Monday, September 2, 2019 2:14 PM
    Monday, September 2, 2019 12:14 PM

All replies

  • As per my understanding you will need two different overloads one which take in the same parameter of that type and the other one which take a dynamic type object something like:

    private static void MyFunction_Alternative<T>(T MyVariable) where T : System.Enum
    {
        foreach (T MyEnum_Value in System.Enum.GetValues(typeof(T)))
        {
            System.Console.WriteLine(MyEnum_Value);
        }
    }
    
    private static void MyFunction_Alternative<T>(dynamic MyVariable) where T : System.Enum
    {
       T Origen_Type = (T)MyVariable;
       foreach (T MyEnum_Value in System.Enum.GetValues(typeof(T)))
       {
            System.Console.WriteLine(MyEnum_Value);
       }
    }

    and i think it can be more simplified like:

    private static void MyFunction_Alternative<T>() where T : System.Enum
    {
        foreach (T MyEnum_Value in System.Enum.GetValues(typeof(T)))
        {
            System.Console.WriteLine(MyEnum_Value);
        }
    }

    and we can call it like:

    MyFunction_Alternative<SomeEnum>();

    and SomeEnum can be :

    public enum SomeEnum
    {
       One,
       Two
    }


    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered"Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Blog | LinkedIn | Stack Overflow | Facebook
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites



    Monday, September 2, 2019 12:52 PM
  • Thanks but it's not that. I say that in I don't want to have two overloads every time I do a function and that I want a function that detects if I pass T or No. Because you put <T> in both functions and it happens that I don't have T in one of the two.
    • Edited by zequion1 Monday, September 2, 2019 1:50 PM
    Monday, September 2, 2019 1:49 PM
  • For the first question, check this function too:

     

    static void MyFunction<T>( T unused = default( T ) ) where T : Enum

    {

        Type type = typeof( T );

     

        Console.WriteLine( "The values of the Enum '{0}' are:", type.Name );

        foreach( T v in Enum.GetValues( type ) )

        {

            Console.WriteLine( "{0} = {1}", v, Convert.ToInt32( v ) );

        }

    }

     

     

    It can be called with an enum type or with a variable of enum type:

       MyFunction<MyEnum1>( );

     

       MyEnum1 x = . . .;

       MyFunction( x );

     

    • Edited by Viorel_MVP Monday, September 2, 2019 3:47 PM
    • Proposed as answer by Cherkaoui.Mouad Monday, September 2, 2019 9:34 PM
    Monday, September 2, 2019 3:35 PM
  • I think it is not this. I say I need a single standard function that I can use sometimes calling it with <T> and other times passing a variable, and it recovers its type, because I don't have <T>.
    Monday, September 2, 2019 4:13 PM
  • Hi zequion1,

    static void MyFunction<T>( T unused = default( T ) ) where T : Enum

    {

        Type type = typeof(T);

     

        Console.WriteLine("The values of the Enum '{0}' are:", type.Name);

        foreach(T v in Enum.GetValues( type ))

        {

            Console.WriteLine( "{0} = {1}", v, Convert.ToInt32(v));

        }

    }

     

     

    It can be called with an enum type or with a variable of enum type:

       MyFunction<MyEnum1>( );

     

       MyEnum1 x = . . .;

       MyFunction( x );

     

    the solution suggested by Ehssan Sajjad and Viorel_ both are perfect, since the C# compiler will take the type directly from the generic argument or will infer it from the variable passed as parameter which explain the usage example : 

    MyFunction<MyEnum1>();
    
    MyEnum2 x = . . .;
    MyFunction(x);

    with such a signature you deal with the two requirements, however if you want to cast using a type variable you can use Convert.ChangeType for primitive types only.

    Best Regards,

    Mouad.


    Monday, September 2, 2019 8:59 PM
  • Greetings Zequion1.

    I can't help thinking you're over-complicating things. Your MyFunction_ seems perfectly adequate for the job, and simple to use. Like so...

          enum MyEnum { One, Two, Three }
          static void Main(string[] args)
          {
             MyFunction_(typeof(MyEnum));
    
             MyEnum e = MyEnum.One;
             MyFunction_(e.GetType());
          }
    
          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);
             }
          }

    Using it this way seems a lot clearer and easier to see what's going on than messing around with <T> when it's not necessary. For a start, anyone reading the calls to MyFunction_ will see instantly that it works on a type and won't be confused thinking it acts on a variable.

    But maybe that's just me.

    Tuesday, September 3, 2019 4:16 AM
  • It has not been read. I say that I have dozens of functions that are systemic and I have to do two constructures each time, when would it be easy if there were T? (Nulable) or if (T == null) Why don't they do it?
    Tuesday, September 3, 2019 4:41 AM