Setting generic type at runtime

后端 未结 6 1238
无人共我
无人共我 2020-11-27 18:24

I have a class

public class A
{
   public static string B(T obj)
   {
       return TransformThisObjectToAString(obj);
   }
}

The

相关标签:
6条回答
  • 2020-11-27 18:48

    You can't. But you've asked the wrong question for the case provided. In this case (as in 99% of cases) all you actually need is a type constraint.

    Try:

    public class A<T> where T : object
    

    or, if T is a known class, a subclass, or an interface then it would be better to use

    public class A<T> where T : YourAbstractClass
    

    Other type constraints also exist. More details: http://msdn.microsoft.com/en-us/library/d5x73970(VS.80).aspx

    As a general note, when learning a new language, you often have to think broadly about what you want to achieve, not specifically finding what you want to do. This is much like real-world verbal languages. It's the difference between learning German by reading a dictionary and forcing the words into English syntax, or learning the syntax and picking up the words. Yes, a German speaker will understand someone who is speaking out of a dictionary, but the WTF per sentence count will be much higher.

    0 讨论(0)
  • 2020-11-27 19:03

    There's absolutely support for this in the framework and the CLR - just not gracefully in C#. You can accomplish what I think you want, though, with the help of a helper method:

    public class A<T>
    {
        public static string B(T obj)
        {
            return obj.ToString();
        }
    }
    
    public class MyClass
    {
        public static void DoExample()
        {
            Console.WriteLine(ExecuteB("Hi"));
            Console.WriteLine(ExecuteB(DateTime.Now));
        }
    
        public static object ExecuteB(object arg)
        {
            Type arg_type = arg.GetType();
            Type class_type = typeof(MyClass);
            MethodInfo mi = class_type.GetMethod("ExecuteBGeneric", BindingFlags.Static | BindingFlags.Public);
            MethodInfo mi2 = mi.MakeGenericMethod(new Type[] { arg_type });
            return mi2.Invoke(null, new object[] { arg });
        }
    
        public static object ExecuteBGeneric<T>(T arg)
        {
            return A<T>.B(arg);
        }
    
    0 讨论(0)
  • 2020-11-27 19:03

    Trying to substitute type parameter at runtime will defeat the whole purpose of type saftey , which is enforced by C# compiler.C# compiler make sures that type parameter are specified at compile time and there is no ambiguity on type arguments at runtime.I doubt you can substitue type parameter at runtime in Generic Type.Specifying type argument of type "Type" is almost like having an unbound generic type.

    0 讨论(0)
  • 2020-11-27 19:05

    You can't do this directly, but you can use reflection to provide a type parameter of a class at run-time. I haven't tested this, but something like this should work:

    // We want to do something like this:
    //    object o = "Hello"
    //    Type t = o.GetType(); 
    //
    // This is pseudo-code only:
    //    string s = A<t>.B(o); 
    
    string InvokeA(object o) {
      // Specify the type parameter of the A<> type
      Type genericType = typeof(A<>).MakeGenericType(new Type[] { o.GetType() });
      // Get the 'B' method and invoke it:
      object res = genericType.GetMethod("B").Invoke(new object[] { o });
      // Convert the result to string & return it
      return (string)res;
    }
    

    Of course, the question is if this is really what you need - If you don't know anything about the object given as an argument, you could as well write the whole code just using object. However, I can imagine some scenarios where this would be useful, so I guess you can try using this.

    0 讨论(0)
  • 2020-11-27 19:06

    You can't. Generic type identifiers have to be known at compile time.

    edit

    as of other posts, it appears to be possible by dynamicly generating the method and invoking it - which has dangers of course. See Thomas' and Dathan's posts for more inforation.

    0 讨论(0)
  • 2020-11-27 19:06

    I created this helper method based on some of the answers here + else where on the web.

    usage:

    InvokeGenericMethodWithRuntimeGenericArguments( MyMethodWithGenericType<IType>, new[] {MyRuntimeGenericType}, null);
    

    method:

    public static object InvokeGenericMethodWithRuntimeGenericArguments(Action methodDelegate, Type[] runtimeGenericArguments, params object[] parameters)
            {
                if (parameters == null)
                {
                    parameters = new object[0];
                }
                if (runtimeGenericArguments == null)
                {
                    runtimeGenericArguments = new Type[0];
                }
    
                var myMethod = methodDelegate.Target.GetType()
                             .GetMethods()
                             .Where(m => m.Name == methodDelegate.Method.Name)
                             .Select(m => new
                             {
                                 Method = m,
                                 Params = m.GetParameters(),
                                 Args = m.GetGenericArguments()
                             })
                             .Where(x => x.Params.Length == parameters.Length
                                         && x.Args.Length == runtimeGenericArguments.Length
                             )
                             .Select(x => x.Method)
                             .First().MakeGenericMethod(runtimeGenericArguments);
                return myMethod.Invoke(methodDelegate.Target, parameters);
            }
    
    0 讨论(0)
提交回复
热议问题