简体   繁体   中英

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

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

In my project, I have classes Plane , and Car located in MyCompany.MySolution.Vehicle namespace like so

- MyCompany.MySolution.Vehicle
  |+Interfaces
  |-Implementations
    |-Car
    |-Plane

I receive type of the vehicle as a string. So, I get string "Car" which means, I need to get Type Car so I can use that type in a generic method to register it like so:

MyFactory.Register<Car>(carId)

So, MyFactory is static class calling Register() method.

Similarly, I receive string "Plane" which means, I need to get Type Plane so I can use that type in the generic method above to register a Plane.

I tried using something like

MyFactory.Register<Type.GetType("MyCompany.MySolution.Vehicle.Implementations.Car")>(carId)

,but that does not work.

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!

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM