如何在 C# 中实现 LateBinding
我在该类中有一个通用类,我编写了一个方法,该方法应该接受 object
类的对象作为参数。
该函数如下 -
protected void AddNewForm(object o )
{
try
{
o.Show();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
我从另一个类调用该函数。但是当我将此函数称为-
Contact objContact=new Contact();
AddNewForm(objContact);
但它显示该函数中的错误。错误为-
“对象”不包含定义 对于“显示”并且没有扩展方法 “显示”接受第一个参数 可以找到类型“object”(你是吗? 缺少 using 指令或 装配参考?)
如何在 C# Windows 应用程序中实现后期绑定?
谢谢。
I'm having a general class in that class i've written one method which should accept the object
class's object as parameter.
the function is as follows-
protected void AddNewForm(object o )
{
try
{
o.Show();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
and i'm calling this function from another classes . but when i call this function as-
Contact objContact=new Contact();
AddNewForm(objContact);
but it shows the error in that function. the error as-
'object' does not contain a definition
for 'Show' and no extension method
'Show' accepting a first argument of
type 'object' could be found (are you
missing a using directive or an
assembly reference?)
How to implement late binding in C# windows application?
thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用 .NET 4,您可以使用新的
dynamic
关键字:如果您不使用 .NET 4,您将不得不求助于反射。
然后它看起来像这样:
但你真的应该考虑使用通用接口:
If you use .NET 4 you can use the new
dynamic
keyword:If you don't use .NET 4 you will have to resort to reflection.
It then would look something like this:
But you really should consider using a common interface:
对于仍在寻找这种东西的人,我发现 https://andy.edinborough.org/Use-Late-Binding-in-C-Now-without-NET-4-0 非常有帮助,我不会在这里发布全部内容,但它也可在回程机(如果它过时了)
For anyone still looking for this kind of thing I found https://andy.edinborough.org/Use-Late-Binding-in-C-Now-without-NET-4-0 very helpful, I will not post the whole contents here, but it is also available at wayback machine if it ever goes stale