Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否可以使用C+中定义的实例+/C#中的CLI类?_C#_.net_C++ Cli - Fatal编程技术网

是否可以使用C+中定义的实例+/C#中的CLI类?

是否可以使用C+中定义的实例+/C#中的CLI类?,c#,.net,c++-cli,C#,.net,C++ Cli,在C++/CLI中,我在类中定义了一些实例,如下所示 public ref class A { public: int test(){return 0;} }; public ref Class B { public: static A^ a_Instance = gcnew A(); }; private B b_instance = new B(); 在C端,我创建了一个B实例,以便在a#U实例中使用函数,如下所示 public ref class A { publi

在C++/CLI中,我在类中定义了一些实例,如下所示

public ref class A
{
public:
    int test(){return 0;}
};

public ref Class B
{
public:
    static A^ a_Instance = gcnew A();

};
private B b_instance = new B();
在C端,我创建了一个B实例,以便在a#U实例中使用函数,如下所示

public ref class A
{
public:
    int test(){return 0;}
};

public ref Class B
{
public:
    static A^ a_Instance = gcnew A();

};
private B b_instance = new B();

问题是,如果我可以在托管C++类中获得实例创建并使用它中的函数?

< p>这里是一个示例代码,说明如何做到这一点。首先,定义托管类(将它们标记为ref和public)。这应该在C++/CLI项目中编译

// CCLI.h
#pragma once
using namespace System;
namespace CCLI {

    public ref class A
    {
    public:
        int test(){return 0;}
    };

    public ref class B
    {
    public:
        static A^ a_Instance = gcnew A();
    };
}
然后,将程序集添加为对C#项目的引用,您可以这样使用元素:

namespace CSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            CCLI.B b_instance = new CCLI.B();

            CCLI.B.a_Instance.test();
        }
    }
}

两个细节:在C++/CLI中使用的实例是通过静态字段的,因此您希望静态地引用它。此外,如果您为给定的体系结构(32位或64位)编译C++/CLI,则要确保使用它的点网程序集在适当的体系结构(x86或x64)中启动。

什么使您认为这是不可能的?您是否有任何问题或遇到任何错误?