B
bikhod
Hi
Using Reflection, I am able to instantiate an instance of
my class using a constructor that takes a primitive data
type (e.g. string).
I have two other constructors, one of which takes an enum
value (Enum defined by me) and the other takes an object
whose class is defined by me. In both these cases, the
CreateInstance() function call on my assembly instance
fails with "Method not found".
Is it not possible to use Reflection with non-primitive
data types?
Below is the code I am using:
// Dummy class
public class Class2
{
public Class2() {}
}
public class Class1
{
public Class1(string s) {}
public Class1(Foo bar) {}
public Class1(Class2 class2) {}
}
// Reflection code
// object[] args = new object[] { c2 }; // *FAILS*
// object[] args = new object[] { Foo.Bar }; // *FAILS*
object[] args = new object[] { "Test input" };
Assembly assemblyInstance = Assembly.LoadFrom
("ClassLibrary1.dll");
newobj = assemblyInstance.CreateInstance(CLASS_NAME,
true,
BindingFlags.Instance|BindingFlags.InvokeMethod|BindingFlag
s.Public,
null,
args,
null,
null);
// use object
Using Reflection, I am able to instantiate an instance of
my class using a constructor that takes a primitive data
type (e.g. string).
I have two other constructors, one of which takes an enum
value (Enum defined by me) and the other takes an object
whose class is defined by me. In both these cases, the
CreateInstance() function call on my assembly instance
fails with "Method not found".
Is it not possible to use Reflection with non-primitive
data types?
Below is the code I am using:
// Dummy class
public class Class2
{
public Class2() {}
}
public class Class1
{
public Class1(string s) {}
public Class1(Foo bar) {}
public Class1(Class2 class2) {}
}
// Reflection code
// object[] args = new object[] { c2 }; // *FAILS*
// object[] args = new object[] { Foo.Bar }; // *FAILS*
object[] args = new object[] { "Test input" };
Assembly assemblyInstance = Assembly.LoadFrom
("ClassLibrary1.dll");
newobj = assemblyInstance.CreateInstance(CLASS_NAME,
true,
BindingFlags.Instance|BindingFlags.InvokeMethod|BindingFlag
s.Public,
null,
args,
null,
null);
// use object