call a method in an external class

  • Thread starter Thread starter keith
  • Start date Start date
K

keith

I created a class libery which has name space Assembly
and class Assembly and compiled it.

Then created a C# project and called a method in
the external class

e.g.

Assembly dll;
dll=Assembly.LoadFrom(@"c:\app\Assembly.dll");
object obj=dll.CreateInstance("Assembly.Assembly");
obj.func();

When compiling, it said "'object' does not contain a
deifinition for 'func'

But if created a VB.NET project
e.g.

Dim dll As Assembly
dll = System.Reflection.Assembly.LoadFrom
("c:\app\Assembly.dll")
Dim obj As Object
obj = dll.CreateInstance("Assembly.Assembly")
obj.func()

I compiled and ran it without any problem.

Can you tell what is problem?

Thanks
 
Keith,

The reason for this is that the reference is of type object. You have
to cast obj to "Assembly.Assembly" in order to call the method, like this:

// Create the instance.
Assembly.Assembly obj = (Assembly.Assembly)
dll.CreateInstance("Assembly.Assembly");

You can then use obj to call the func method.

Hope this helps.
 
keith said:
I created a class libery which has name space Assembly
and class Assembly and compiled it.

Then created a C# project and called a method in
the external class

e.g.

Assembly dll;
dll=Assembly.LoadFrom(@"c:\app\Assembly.dll");
object obj=dll.CreateInstance("Assembly.Assembly");
obj.func();

When compiling, it said "'object' does not contain a
deifinition for 'func'

But if created a VB.NET project
e.g.

Dim dll As Assembly
dll = System.Reflection.Assembly.LoadFrom
("c:\app\Assembly.dll")
Dim obj As Object
obj = dll.CreateInstance("Assembly.Assembly")
obj.func()

I compiled and ran it without any problem.

Can you tell what is problem?

Are you sure func is an instance member and not a class member
 
Nicholas,

This will not work since the Assembly.Assembly is
an external class in an dll file that dosen't have
reference in my project that will use it.

Thanks
Keith

-----Original Message-----
Keith,

The reason for this is that the reference is of type object. You have
to cast obj to "Assembly.Assembly" in order to call the method, like this:

// Create the instance.
Assembly.Assembly obj = (Assembly.Assembly)
dll.CreateInstance("Assembly.Assembly");

You can then use obj to call the func method.

Hope this helps.


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

keith said:
I created a class libery which has name space Assembly
and class Assembly and compiled it.

Then created a C# project and called a method in
the external class

e.g.

Assembly dll;
dll=Assembly.LoadFrom(@"c:\app\Assembly.dll");
object obj=dll.CreateInstance("Assembly.Assembly");
obj.func();

When compiling, it said "'object' does not contain a
deifinition for 'func'

But if created a VB.NET project
e.g.

Dim dll As Assembly
dll = System.Reflection.Assembly.LoadFrom
("c:\app\Assembly.dll")
Dim obj As Object
obj = dll.CreateInstance("Assembly.Assembly")
obj.func()

I compiled and ran it without any problem.

Can you tell what is problem?

Thanks


.
 
Keith,

In that case, you have two options. The first is to have a base class
which exposes the methods of Assembly.Assembly which is accessible to both
Assembly.dll and to the code loading it dynamically, and then cast to the
base type. You can also do this with an interface. The key is to have both
assemblies able to access the interface/base class definition, and the one
loading Assembly.Assembly dynamically doesn't have to access the class that
has the implementation directly.

The only other option is to use reflection to call the method.


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

keith said:
Nicholas,

This will not work since the Assembly.Assembly is
an external class in an dll file that dosen't have
reference in my project that will use it.

Thanks
Keith

-----Original Message-----
Keith,

The reason for this is that the reference is of type object. You have
to cast obj to "Assembly.Assembly" in order to call the method, like this:

// Create the instance.
Assembly.Assembly obj = (Assembly.Assembly)
dll.CreateInstance("Assembly.Assembly");

You can then use obj to call the func method.

Hope this helps.


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

keith said:
I created a class libery which has name space Assembly
and class Assembly and compiled it.

Then created a C# project and called a method in
the external class

e.g.

Assembly dll;
dll=Assembly.LoadFrom(@"c:\app\Assembly.dll");
object obj=dll.CreateInstance("Assembly.Assembly");
obj.func();

When compiling, it said "'object' does not contain a
deifinition for 'func'

But if created a VB.NET project
e.g.

Dim dll As Assembly
dll = System.Reflection.Assembly.LoadFrom
("c:\app\Assembly.dll")
Dim obj As Object
obj = dll.CreateInstance("Assembly.Assembly")
obj.func()

I compiled and ran it without any problem.

Can you tell what is problem?

Thanks


.
 
I'm sure.

I did use same method in an external class (a dll file).

Then I tried call the method from both C# and VB.NET
by using almost same codes. It worked for VB but not
in C#.
 
My purpose is to call a method in a external file
dynamically. So I don't want to use base class or
interface.

I did use reflection in both VB.NET and
C# project. used almost same codes (please compare
the codes in my first email) to load and create instance,
finally call the method.

It worked for VB.NET but not C#. It surprised me since I
should be able to do samething in either language.


Thanks

Keith





-----Original Message-----
Keith,

In that case, you have two options. The first is to have a base class
which exposes the methods of Assembly.Assembly which is accessible to both
Assembly.dll and to the code loading it dynamically, and then cast to the
base type. You can also do this with an interface. The key is to have both
assemblies able to access the interface/base class definition, and the one
loading Assembly.Assembly dynamically doesn't have to access the class that
has the implementation directly.

The only other option is to use reflection to call the method.


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

keith said:
Nicholas,

This will not work since the Assembly.Assembly is
an external class in an dll file that dosen't have
reference in my project that will use it.

Thanks
Keith

-----Original Message-----
Keith,

The reason for this is that the reference is of
type
object. You have
to cast obj to "Assembly.Assembly" in order to call
the
method, like this:
// Create the instance.
Assembly.Assembly obj = (Assembly.Assembly)
dll.CreateInstance("Assembly.Assembly");

You can then use obj to call the func method.

Hope this helps.


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

I created a class libery which has name space Assembly
and class Assembly and compiled it.

Then created a C# project and called a method in
the external class

e.g.

Assembly dll;
dll=Assembly.LoadFrom(@"c:\app\Assembly.dll");
object obj=dll.CreateInstance("Assembly.Assembly");
obj.func();

When compiling, it said "'object' does not contain a
deifinition for 'func'

But if created a VB.NET project
e.g.

Dim dll As Assembly
dll = System.Reflection.Assembly.LoadFrom
("c:\app\Assembly.dll")
Dim obj As Object
obj = dll.CreateInstance("Assembly.Assembly")
obj.func()

I compiled and ran it without any problem.

Can you tell what is problem?

Thanks


.


.
 
Keith,

In VB, if you make a method call on type object, it will perform all of
the reflection work for you. VB and C# can do the same things, they just go
about it differently.


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

keith said:
My purpose is to call a method in a external file
dynamically. So I don't want to use base class or
interface.

I did use reflection in both VB.NET and
C# project. used almost same codes (please compare
the codes in my first email) to load and create instance,
finally call the method.

It worked for VB.NET but not C#. It surprised me since I
should be able to do samething in either language.


Thanks

Keith





-----Original Message-----
Keith,

In that case, you have two options. The first is to have a base class
which exposes the methods of Assembly.Assembly which is accessible to both
Assembly.dll and to the code loading it dynamically, and then cast to the
base type. You can also do this with an interface. The key is to have both
assemblies able to access the interface/base class definition, and the one
loading Assembly.Assembly dynamically doesn't have to access the class that
has the implementation directly.

The only other option is to use reflection to call the method.


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

keith said:
Nicholas,

This will not work since the Assembly.Assembly is
an external class in an dll file that dosen't have
reference in my project that will use it.

Thanks
Keith


-----Original Message-----
Keith,

The reason for this is that the reference is of type
object. You have
to cast obj to "Assembly.Assembly" in order to call the
method, like this:

// Create the instance.
Assembly.Assembly obj = (Assembly.Assembly)
dll.CreateInstance("Assembly.Assembly");

You can then use obj to call the func method.

Hope this helps.


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

I created a class libery which has name space Assembly
and class Assembly and compiled it.

Then created a C# project and called a method in
the external class

e.g.

Assembly dll;
dll=Assembly.LoadFrom(@"c:\app\Assembly.dll");
object obj=dll.CreateInstance("Assembly.Assembly");
obj.func();

When compiling, it said "'object' does not contain a
deifinition for 'func'

But if created a VB.NET project
e.g.

Dim dll As Assembly
dll = System.Reflection.Assembly.LoadFrom
("c:\app\Assembly.dll")
Dim obj As Object
obj = dll.CreateInstance("Assembly.Assembly")
obj.func()

I compiled and ran it without any problem.

Can you tell what is problem?

Thanks


.


.
 
Nicholas,

How to get samething work for C#?

Keith
-----Original Message-----
Keith,

In VB, if you make a method call on type object, it will perform all of
the reflection work for you. VB and C# can do the same things, they just go
about it differently.


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

keith said:
My purpose is to call a method in a external file
dynamically. So I don't want to use base class or
interface.

I did use reflection in both VB.NET and
C# project. used almost same codes (please compare
the codes in my first email) to load and create instance,
finally call the method.

It worked for VB.NET but not C#. It surprised me since I
should be able to do samething in either language.


Thanks

Keith





-----Original Message-----
Keith,

In that case, you have two options. The first is
to
have a base class
which exposes the methods of Assembly.Assembly which
is
accessible to both
Assembly.dll and to the code loading it dynamically,
and
then cast to the
base type. You can also do this with an interface.
The
key is to have both
assemblies able to access the interface/base class definition, and the one
loading Assembly.Assembly dynamically doesn't have to access the class that
has the implementation directly.

The only other option is to use reflection to call the method.


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

Nicholas,

This will not work since the Assembly.Assembly is
an external class in an dll file that dosen't have
reference in my project that will use it.

Thanks
Keith


-----Original Message-----
Keith,

The reason for this is that the reference is of type
object. You have
to cast obj to "Assembly.Assembly" in order to call the
method, like this:

// Create the instance.
Assembly.Assembly obj = (Assembly.Assembly)
dll.CreateInstance("Assembly.Assembly");

You can then use obj to call the func method.

Hope this helps.


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

I created a class libery which has name space Assembly
and class Assembly and compiled it.

Then created a C# project and called a method in
the external class

e.g.

Assembly dll;
dll=Assembly.LoadFrom(@"c:\app\Assembly.dll");
object obj=dll.CreateInstance ("Assembly.Assembly");
obj.func();

When compiling, it said "'object' does not contain a
deifinition for 'func'

But if created a VB.NET project
e.g.

Dim dll As Assembly
dll = System.Reflection.Assembly.LoadFrom
("c:\app\Assembly.dll")
Dim obj As Object
obj = dll.CreateInstance("Assembly.Assembly")
obj.func()

I compiled and ran it without any problem.

Can you tell what is problem?

Thanks


.



.


.
 
Keith,

You must use reflection, getting the MethodInfo instance for the method,
and then Invoking it.

You really should consider using an interface, it's just better design,
and you get the type safety.

I should ammend my statement from before, VB and C# can't do everything
the other can do, but most things.

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

keith said:
Nicholas,

How to get samething work for C#?

Keith
-----Original Message-----
Keith,

In VB, if you make a method call on type object, it will perform all of
the reflection work for you. VB and C# can do the same things, they just go
about it differently.


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

keith said:
My purpose is to call a method in a external file
dynamically. So I don't want to use base class or
interface.

I did use reflection in both VB.NET and
C# project. used almost same codes (please compare
the codes in my first email) to load and create instance,
finally call the method.

It worked for VB.NET but not C#. It surprised me since I
should be able to do samething in either language.


Thanks

Keith






-----Original Message-----
Keith,

In that case, you have two options. The first is to
have a base class
which exposes the methods of Assembly.Assembly which is
accessible to both
Assembly.dll and to the code loading it dynamically, and
then cast to the
base type. You can also do this with an interface. The
key is to have both
assemblies able to access the interface/base class
definition, and the one
loading Assembly.Assembly dynamically doesn't have to
access the class that
has the implementation directly.

The only other option is to use reflection to call
the method.


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

Nicholas,

This will not work since the Assembly.Assembly is
an external class in an dll file that dosen't have
reference in my project that will use it.

Thanks
Keith


-----Original Message-----
Keith,

The reason for this is that the reference is of
type
object. You have
to cast obj to "Assembly.Assembly" in order to call
the
method, like this:

// Create the instance.
Assembly.Assembly obj = (Assembly.Assembly)
dll.CreateInstance("Assembly.Assembly");

You can then use obj to call the func method.

Hope this helps.


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

I created a class libery which has name space
Assembly
and class Assembly and compiled it.

Then created a C# project and called a method in
the external class

e.g.

Assembly dll;
dll=Assembly.LoadFrom(@"c:\app\Assembly.dll");
object obj=dll.CreateInstance ("Assembly.Assembly");
obj.func();

When compiling, it said "'object' does not contain a
deifinition for 'func'

But if created a VB.NET project
e.g.

Dim dll As Assembly
dll = System.Reflection.Assembly.LoadFrom
("c:\app\Assembly.dll")
Dim obj As Object
obj = dll.CreateInstance("Assembly.Assembly")
obj.func()

I compiled and ran it without any problem.

Can you tell what is problem?

Thanks


.



.


.
 
Back
Top