Working with assemblies

M

Max

I made a class library which I want to load when something happens.
How can I load this library and then pass in a paramter?

I tried:

Assembly asm = Assembly.Load("mylibrary");
Type t = asm.GetType("MyClass");
Object obj = Activator.CreateInstance(t, new object[] {myargument});

and I get an exception:

Exception caught: System.ArgumentNullException: Value cannot be null.
Parameter name: type
at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[]
activationAttributes)
at System.Activator.CreateInstance(Type type, Object[] args)
 
N

Nicholas Paldino [.NET/C# MVP]

Max,

The reason you are getting this is that the type, t, is null. When you
make the call to GetType (on the Assembly instance asm), you need to pass
the namespace-qualified type name. Change the string to that, and it should
work.

Hope this helps.
 
M

Max

What is the namespace-qualified type name?
I changed the code to this and t is still null.

Assembly asm = Assembly.Load("mylibraryfile");
Type t = asm.GetType("mylibraryNamespace");
Object obj = Activator.CreateInstance(t, new object[] {myargument});


Nicholas Paldino said:
Max,

The reason you are getting this is that the type, t, is null. When you
make the call to GetType (on the Assembly instance asm), you need to pass
the namespace-qualified type name. Change the string to that, and it should
work.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Max said:
I made a class library which I want to load when something happens.
How can I load this library and then pass in a paramter?

I tried:

Assembly asm = Assembly.Load("mylibrary");
Type t = asm.GetType("MyClass");
Object obj = Activator.CreateInstance(t, new object[] {myargument});

and I get an exception:

Exception caught: System.ArgumentNullException: Value cannot be null.
Parameter name: type
at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr,
Binder binder, Object[] args, CultureInfo culture, Object[]
activationAttributes)
at System.Activator.CreateInstance(Type type, Object[] args)
 
W

Willy Denoyette [MVP]

Max said:
I made a class library which I want to load when something happens.
How can I load this library and then pass in a paramter?

I tried:

Assembly asm = Assembly.Load("mylibrary");
Type t = asm.GetType("MyClass");
Object obj = Activator.CreateInstance(t, new object[] {myargument});

and I get an exception:

Exception caught: System.ArgumentNullException: Value cannot be null.
Parameter name: type
at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr,
Binder binder, Object[] args, CultureInfo culture, Object[]
activationAttributes)
at System.Activator.CreateInstance(Type type, Object[] args)

That means that t is null. Are you sure "MyClass" exists in "myLibary"?

Willy.
 
N

Nicholas Paldino [.NET/C# MVP]

Max,

You need the namespace and the type, like this:

Type t = asm.GetType("mylibrarynamespace.mytype");


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Max said:
What is the namespace-qualified type name?
I changed the code to this and t is still null.

Assembly asm = Assembly.Load("mylibraryfile");
Type t = asm.GetType("mylibraryNamespace");
Object obj = Activator.CreateInstance(t, new object[] {myargument});


Nicholas Paldino said:
Max,

The reason you are getting this is that the type, t, is null. When
you make the call to GetType (on the Assembly instance asm), you need to
pass the namespace-qualified type name. Change the string to that, and
it should work.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Max said:
I made a class library which I want to load when something happens.
How can I load this library and then pass in a paramter?

I tried:

Assembly asm = Assembly.Load("mylibrary");
Type t = asm.GetType("MyClass");
Object obj = Activator.CreateInstance(t, new object[] {myargument});

and I get an exception:

Exception caught: System.ArgumentNullException: Value cannot be null.
Parameter name: type
at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr,
Binder binder, Object[] args, CultureInfo culture, Object[]
activationAttributes)
at System.Activator.CreateInstance(Type type, Object[] args)
 
M

Max

Yes the class exists.
Anyways I got it to work by getting all the Types and then getting the method I wanted...




Willy Denoyette said:
Max said:
I made a class library which I want to load when something happens.
How can I load this library and then pass in a paramter?

I tried:

Assembly asm = Assembly.Load("mylibrary");
Type t = asm.GetType("MyClass");
Object obj = Activator.CreateInstance(t, new object[] {myargument});

and I get an exception:

Exception caught: System.ArgumentNullException: Value cannot be null.
Parameter name: type
at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr,
Binder binder, Object[] args, CultureInfo culture, Object[]
activationAttributes)
at System.Activator.CreateInstance(Type type, Object[] args)

That means that t is null. Are you sure "MyClass" exists in "myLibary"?

Willy.
 

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

Top