C# Getting Type out of a string variable and using it in generic method

前端 未结 3 1331
灰色年华
灰色年华 2021-01-29 05:59

I want to be able to get the actual Type of a string value I receive by some means (i.e from database) so I can use that Type in generic method like DoSomething

相关标签:
3条回答
  • 2021-01-29 06:24

    You can use a dictionary which holds all your types with strings keys:

    var d = new Dictionary<String,Type>(); 
    d.Add("Car",typeof(Car)); 
    d.Add("Plane",typeof(Plane)); 
    

    Then if you get the string "Car" from the database you can get the type like this:

    var myType = d["Car"]; 
    

    Then use myType as the real Type.

    0 讨论(0)
  • If you want to invoke a Generic Method with a Type parameter generate at Runtime, you could do something like this:

    var vehicleString = "Car";
    
    // use the fully-qualified name of the type here
    // (assuming Car is in the same assembly as this code, 
    //  if not add a ", TargetAssemblyName" to the end of the string)
    var vehicleType = 
        Type.GetType($"MyCompany.MySolution.Vehicle.Implementations.{vehicleString}");
    
    // assuming MyFactory is the name of the class 
    // containing the Register<T>-Method
    typeof(MyFactory).GetMethod("Register")
        .MakeGenericMethod(vehicleType)
        .Invoke(this);
    

    Working Example

    Just as a note:

    This is not how generics are supposed to be used. I'm just pointing out the possibility, not giving you an ideal answer to the problem you're proposing. Maybe you should rethink some of your architectural design choices!

    0 讨论(0)
  • 2021-01-29 06:33

    If Register<T> does something like this

    void Register<T>(int id)
    {
        _dictionary.Add(typeof(T), ...);
    }
    

    Create a non generic overload

    void Register(Type t, int id)
    {
        _dictionary.Add(t, ...);
    }
    

    This new overload is not type safe, but creating a type out of a string isn't anyway.

    The purpose of generics is to gain variability (not to be confused with dynamic behavior!) while keeping type safety. But when types are determined at runtime, type safety is not given and generics are more of a hindrance than useful.

    Note that type safety is ensured by the compiler, which does of course not work at runtime.

    0 讨论(0)
提交回复
热议问题