.NET中的互操作性 [英] interoperability in .net

查看:54
本文介绍了.NET中的互操作性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


假设我有用C#编写的库文件,其中有2种方法.众所周知,C#是区分大小写的语言.我们可以用相同的名称编写两个方法.例如.

Class MyClass
{
   public int Mymethod()
   {
     return 1;
   }

   public int mymethod()
   {
     return 2;
   }
}


如果要在vb.net项目中使用该库文件,是否可以使用?
如果VB.NET不区分大小写,如何实现.net互操作性?
我认为编译器会放置一些代码,或者我们在编译库文件时必须指定一些参数.我不确定我的答案.请清除我的怀疑.

谢谢,

解决方案

是的,您可以在VB.Net中使用C#类.之所以能够实现,是因为所有.Net语言最终都针对通用类型系统.
此处 [ [ http://msdn.microsoft.com/en-us/library/12a7a7h3 (v = vs.71).aspx [
和:

唯一性:在符合CLS的范围内的所有名称都必须是唯一的..."

因此,基本上,您提出的类将不符合CLS,因此不能与其他.NET语言互操作.该文档不能保证您尝试这样做会发生什么. (希望您在此过程中的某处会得到可理解的错误消息.)


Hi,
Assume that I have library file written in C#, which has 2 methods. As we know C# is case sensitive language. We can write two methods with same name. For eg.

Class MyClass
{
   public int Mymethod()
   {
     return 1;
   }

   public int mymethod()
   {
     return 2;
   }
}


If I want to use this library file into my vb.net project, Will it allow me to that?
How come .net interoperability will be achieved as if VB.NET is not case sensitive?
I think compiler will put some code or we have to specify some parameter while compiling library file. I not sure about my answer. Please clear my doubt.

Thanks,

Yes you can use a C# class in VB.Net. This can be achieved because all .Net languages ultimately target the Common Type System.
Read more about language interoperability
here[^].


Actually, you are right, that will cause a problem.

If you follow the link that Abhinav S gave you, you''ll see another link to:

http://msdn.microsoft.com/en-us/library/12a7a7h3(v=vs.71).aspx[^]

Which gives the Common Language Specification -- basically the set of rules you must follow in order for your class to be guaranteed to interoperate with other .NET languages.

In particular it states:

"Characters and casing: ...For two identifiers to be considered distinct, they must differ by more than just their case."

and:

"Uniqueness: All names within a CLS-compliant scope must be distinct..."

So, basically the class you propose would not be CLS-compliant and therefore would not be interoperable with other .NET languages. The documentation does not guarantee what will happen if you try to do that. (Hopefully you''d end up with an intelligible error message somewhere along the way.)


这篇关于.NET中的互操作性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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