⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 inherit.txt

📁 Vc.Net入门与提高源码
💻 TXT
字号:
Cross language inheritance in .NET
Chris Maunder, The Code Project (www.codeproject.com)

The introduction of .NET brings makes cross language development simpler
than ever. No longer must one worry about using COM wrappers in C++ to
access VB components, or calling conventions when attempting to interface
with FORTRAN code. All .NET enabled languages are now first class entities
and interoperate seamlessly, allowing disparate systems to work together
more easily than ever, and also allowing developers with different skill
sets to work more harmoneously together on projects.

Creating a component in one language that can be used by another language
is very simple. Use the Visual Studio.NET Wizard to create a class library
in your favourite .NET language, compile it, and you are done. 

For example, we will create a simple class in C# that exposes two methods:

namespace MyCSClass
{
	using System;

	public class CSClass
	{
		public CSClass() { }

		// returns the length of the passed string
		public int MyMethod(string str)
		{
			return str.Length;
		}

		// returns n squared
		virtual public int MyVirtualMethod(int n)
		{
			return n*n;
		}
	}
}

The first method, MyMethod, takes a string object and returns its length. 
The second method is virtual, and returns the square of the number passed
in. Assume that we have compiled this component to MyCSClass.dll

To consume this component using managed C++ we need to first import the
assembly into our program:

	#using "MyCSClass.dll"

That's it. No typelibs, no .def files, no ATL headers. We simply use the
#using statement and let the compiler do the rest.

Once we have imported the class we can then use the 'using' declaration to
save typing:

	using namespace MyCSClass;

Actually consuming the class is the same as using any other managed referece
type in .NET:

	CSClass *cs = new CSClass();
	int result = cs->MyMethod("Hello World");

This will return the value '11'

Cross language interoperability goes further than simply easing the use of
components. We can also inherit new classes from components written in other
languages. 

The method CSClass::MyVirtualMethod is virtual, so let's declare a new C++
class that is derived from this C# class, and override that virtual method

__gc class CPPClass : public MyCSClass::CSClass
{
public:
	// returns the cube of the given number
	virtual int MyVirtualMethod(int n)
	{
		return n*n*n;
	}
};

We have overridden CSClass::MyVirtualMethod with our new method that returns
the cube, not the square, of the given number. Once we compile the code we 
we have a new C++ component that overrides the virtual method in the C# class,
and also has the non-virtual 'MyMethod()' function.

	CPPClass *cpp = new CPPClass();

	int result;
	result = cpp->MyVirtualMethod(2);
	result = cpp->MyMethod("Hello World");

Cross language interoperability allows you to extend 3rd components with your
own functionality in an object oriented fashion. You can easily work with
components in any CLS compliant language, and when debugging you are able to
step through function calls between components and between languages in the 
same application. Exception handling is also consistent across languages. If
a component in one language throws an exception it can be caught and handled 
by code written in another language. Most importantly it allows you and your
team freedom to choose the language they wish to work in. 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -