refelection (property)

  • Thread starter Thread starter csharpula csharp
  • Start date Start date
C

csharpula csharp

Hello ,
I want to get a property object using reflection by passing the string
name of this property.
I tried :

Type objType = asm.GetType(variableName);
object var= Activator.CreateInstance(objType);

But it's not working.Maybe because i am passing only variable name
(String) and not the whole path to it. How do I implement this and where
can I fetch this path if I need it?

Thank u!
 
Can you define what you mean by a "property object"? Your code is
locating a type by name (from the assembly held in "asm"), and then
creating an instance of that type.

When looking inside an assembly, you still need to pass the FullName
(i.e. namespace + name: "System.String"); or you can use
AssemblyQualifiedName without having to get the assembly first.

Marc
 
I want to get a property object using reflection by passing the string
name of this property.
I tried :

Type objType = asm.GetType(variableName);
object var= Activator.CreateInstance(objType);

But it's not working.Maybe because i am passing only variable name
(String) and not the whole path to it. How do I implement this and where

Can you elaborate on "only variable name" and "not the whole path"?
What is the whole path that you're referring to?

Try:

Assembly asm = Assembly.Load("NameOfAssembly");
Type t = asm.GetType("Fully.Qualified.Name.Of.Class");

object instance = Activator.CreateInstance(t);
// -- OR --

object instance = t.GetConstructor(Type.EmptyTypes).Invoke(null);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top