I'm passing a type to a method and creating an object based on that type. Assume argType
is a Type
and passed to the method as a parameter.
var assembly = AppDomain.CurrentDomain.GetAssemblies().Single(a => a.GetName().Name == "MyAssembly");
var newType = assembly.GetTypes().SingleOrDefault(x => x.FullName == argType.FullName + "Suffix");
var newObject = Activator.CreateInstance(newType);
This works fine for most objects, but if I pass a type with a generic sub type (e.g. MyClass<MyType>
), it fails [I'd need to create a MyClassSuffix<MyType>
].
How could the code above be improved so that generic types (ending with "Suffix") can also be created? I looked at the FullName
property for such types which contain substrings like '1[[
. I'd rather not do regex parsing to append "Suffix" before these characters begin. I'm thinking there is a better way.
Aucun commentaire:
Enregistrer un commentaire