Hi!
How can I receive a string value, find an Entity with the same name and use this Entity as a type?
Thanks!!
Hi!
How can I receive a string value, find an Entity with the same name and use this Entity as a type?
Thanks!!
If you have a Type in an assembly called "RipperTest.dll" and it's namespace is "RipperTest" you would put the fully qualified assembly name with as the type string.
Before using this type you have to be sure it's assembly is loaded if it's not in the calling assembly.
for a class called "Ripper1" in the RipperTest namespace in the RipperTest.dll
namespace RipperTest
{
[Serializable]
public class Ripper1 : RipperBase
{
}
}
Type myType = Type.GetType("RipperTest.Ripper1");
and then to later use this type
var newObject = Activator.CreateInstance(myType);
If your original instance implements a specific interface you can cast to that interface instead of returning an object.
like so:
var newObject = (IRipper)Activator.CreateInstance(myType);
If you're trying to return a list of classes from your current assembly you can do something like this:
Assembly execAsm = Assembly.GetExecutingAssembly();
Type myType = (from type in execAsm.GetTypes()
where type.Name.Equals("YourTypeNameHere")
select type).FirstOrDefault();