Creating an instance of type using string litteral
-
Hey, I have read this before it is possible to create an instance of a type by specifying their string literal equivalent. for example if i need to create an instance of Employee class generally i will say Employee objEmployee = new Employee(); my question is i just know that the class which i need to instantiate is "Employee" how will i instantiate using the string "Employee" hope i did not made the simple ques more complex. Cheers, Venkatraman Kalyanam Chennai - India "Being Excellent is not a skill, it is an attitude" Reality is an illusion caused by caffeine deficiency(one Microsoft Research scholor)
-
Hey, I have read this before it is possible to create an instance of a type by specifying their string literal equivalent. for example if i need to create an instance of Employee class generally i will say Employee objEmployee = new Employee(); my question is i just know that the class which i need to instantiate is "Employee" how will i instantiate using the string "Employee" hope i did not made the simple ques more complex. Cheers, Venkatraman Kalyanam Chennai - India "Being Excellent is not a skill, it is an attitude" Reality is an illusion caused by caffeine deficiency(one Microsoft Research scholor)
Check out AppDomain.CreateInstance and AppDomain.CreateInstanceFrom Rocky Moore <><
-
Hey, I have read this before it is possible to create an instance of a type by specifying their string literal equivalent. for example if i need to create an instance of Employee class generally i will say Employee objEmployee = new Employee(); my question is i just know that the class which i need to instantiate is "Employee" how will i instantiate using the string "Employee" hope i did not made the simple ques more complex. Cheers, Venkatraman Kalyanam Chennai - India "Being Excellent is not a skill, it is an attitude" Reality is an illusion caused by caffeine deficiency(one Microsoft Research scholor)
You can instantiate a class using a "partial" name... i.e. the type name as a string literal, one way is...
string path = "MyApp.MyAssembly"; // assembly name string class = "MyClass"; // class in the assembly object MyObj = Assembly.Load(path).CreateInstance(class);
This is most useful when your class implements a known interface but you want to dynamically load the implementation because you can do the following.IFooBar fooBar = Assembly.Load(path).CreateInstance(class) as IFooBar
This stuff is my favourite thing about .net... :-D :cool: HTH Shaun :-D ----------------------------------------------------------------------- Shaun Austin: .NET Specialist. Spreading the word of .NET to the world... well the UK... well my tiny corner of it!! :-D -
Hey, I have read this before it is possible to create an instance of a type by specifying their string literal equivalent. for example if i need to create an instance of Employee class generally i will say Employee objEmployee = new Employee(); my question is i just know that the class which i need to instantiate is "Employee" how will i instantiate using the string "Employee" hope i did not made the simple ques more complex. Cheers, Venkatraman Kalyanam Chennai - India "Being Excellent is not a skill, it is an attitude" Reality is an illusion caused by caffeine deficiency(one Microsoft Research scholor)
Venkat, Have a look at CreateInstance and CreateInstanceAndUnwrap method, I think you can also do this using other reflection techniques. Cheers, Kannan
-
Venkat, Have a look at CreateInstance and CreateInstanceAndUnwrap method, I think you can also do this using other reflection techniques. Cheers, Kannan
thanks for your info Cheers, Venkatraman Kalyanam Chennai - India "Being Excellent is not a skill, it is an attitude" Reality is an illusion caused by caffeine deficiency(one Microsoft Research scholor)
-
You can instantiate a class using a "partial" name... i.e. the type name as a string literal, one way is...
string path = "MyApp.MyAssembly"; // assembly name string class = "MyClass"; // class in the assembly object MyObj = Assembly.Load(path).CreateInstance(class);
This is most useful when your class implements a known interface but you want to dynamically load the implementation because you can do the following.IFooBar fooBar = Assembly.Load(path).CreateInstance(class) as IFooBar
This stuff is my favourite thing about .net... :-D :cool: HTH Shaun :-D ----------------------------------------------------------------------- Shaun Austin: .NET Specialist. Spreading the word of .NET to the world... well the UK... well my tiny corner of it!! :-Dthanks for your info Cheers, Venkatraman Kalyanam Chennai - India "Being Excellent is not a skill, it is an attitude" Reality is an illusion caused by caffeine deficiency(one Microsoft Research scholor)
-
Hey, I have read this before it is possible to create an instance of a type by specifying their string literal equivalent. for example if i need to create an instance of Employee class generally i will say Employee objEmployee = new Employee(); my question is i just know that the class which i need to instantiate is "Employee" how will i instantiate using the string "Employee" hope i did not made the simple ques more complex. Cheers, Venkatraman Kalyanam Chennai - India "Being Excellent is not a skill, it is an attitude" Reality is an illusion caused by caffeine deficiency(one Microsoft Research scholor)