在外部类中调用方法 [英] call a method in an external class

查看:75
本文介绍了在外部类中调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个类库,它有名称空间Assembly

和类汇编并编译它。


然后创建一个C#项目并调用一个方法

外部类


例如


汇编dll;

dll =汇编.LoadFrom(@" c:\\\\Assembly.dll");

object obj = dll.CreateInstance(" Assembly.Assembly");

obj.func();


编译时,它说''''''''''''''''''''''''''''''''''''''''''''''''' br />

但是如果创建一个VB.NET项目

例如


Dim dll As Assembly

dll = System.Reflection.Assembly.LoadFrom

(" c:\\\Assembly.dll")

Dim obj As Object

obj = dll.CreateInstance(" Assembly.Assembly")

obj.func()


我编译并运行它没有任何问题。


你能说出什么是问题吗?


谢谢

解决方案

基思,


原因这是引用是object类型。您有ob />
将obj强制转换为Assembly.Assembly为了调用方法,如下所示:


//创建实例。

Assembly.Assembly obj =(Assembly.Assembly)

dll.CreateInstance(" Assembly.Assembly");


然后你可以使用obj来调用func方法。


希望这会有所帮助。

-

- Nicholas Paldino [.NET / C#MVP]

- ni ************** @ exisconsulting.com

" keith" <柯***** @ shaw.ca>在消息中写道

news:02 **************************** @ phx.gbl ... < blockquote class =post_quotes>我创建了一个类libery,它具有名称空间Assembly
和类汇编并编译它。

然后创建了一个C#项目并调用了一个方法
外部类



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

编译时,它说'''对象''没有包含对''func'的定义

但如果创建了一个VB.NET项目


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

我编译并运行它没有任何问题。

你能说出什么是问题吗?
谢谢





keith写道:

我创建了一个类libery,它有名称空间Assembly和/或类汇编并编译它。

然后创建了一个C#项目并在外部类中调用了一个方法

例如

汇编dll;
dll = Assembly.LoadFrom(@" c:\\\\Assembly.dll");
对象obj = dll.CreateInstance(" Assembly.Assembly");
obj.func();

编译时,它说'''对象''不包含
'func'的定义'

但如果创建了一个VB.NET项目


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

我编译并运行它没有任何问题。

你能来吗?告诉我有什么问题?




你确定func是一个实例成员而不是一个班级成员

-


Martin Honnen
http://JavaScript.FAQTs.com/


Nicholas,


自从Assembly.Assembly是
以后,这是行不通的。
一个dll文件中的外部类,在我的项目中没有使用

引用它将使用它。


谢谢

基思

-----原帖-----
基思,

原因为此,引用的类型为
object。你必须将obj强制转换为Assembly.Assembly。为了调用
方法,如下所示:
//创建实例。
Assembly.Assembly obj =(Assembly.Assembly)
dll.CreateInstance(" Assembly。汇编);

然后你可以使用obj来调用func方法。

希望这会有所帮助。

-
- Nicholas Paldino [.NET / C#MVP]
- ni ******* *******@exisconsulting.com

基思 <柯***** @ shaw.ca>在消息中写道
新闻:02 **************************** @ phx.gbl ...

我创建了一个类libery,它有名称空间Assembly
和类汇编并编译它。

然后创建了一个C#项目并在外部类中调用了一个方法<

汇编dll;
dll = Assembly.LoadFrom(@" c:\\\\Assembly.dll");
对象obj = dll.CreateInstance(" Assembly.Assembly");
obj.func();

编译时,它说'''对象''不包含

但如果创建了一个VB.NET项目


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

我编译并运行它没有任何问题。

你能说出什么是问题吗?
谢谢





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.
--
- Nicholas Paldino [.NET/C# MVP]
- ni**************@exisconsulting.com

"keith" <ke*****@shaw.ca> wrote in message
news:02****************************@phx.gbl...

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 wrote:

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
--

Martin Honnen
http://JavaScript.FAQTs.com/


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 haveto 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]
- ni**************@exisconsulting.com

"keith" <ke*****@shaw.ca> wrote in message
news:02****************************@phx.gbl...

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


.



这篇关于在外部类中调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆