简体   繁体   English

Typedef,C ++ / CLI,C#+依赖项注入

[英]Typedefs, C++/CLI, C# + dependency injection

I have a specialized dictionary written in C# that takes two generic arguments. 我有一个用C#编写的专用字典,它带有两个通用参数。 The interface is 界面是

public interface IMyDictionary<TKey, TValue> { ... }

and one of it's implementations 它的实现之一

public MyDictionary<TKey, TValue> {...}

I also have a complicated C++ template structure which I typedef in my C++/CLI ref class: 我也有一个复杂的C ++模板结构,可以在我的C ++ / CLI ref类中键入:

typedef boost::some_complex_template<stuff>    MySimpleValueType;

In my C++/CLI ref class constructor, I used to create an instance of the dictionary: 在我的C ++ / CLI ref类构造函数中,我用来创建字典的实例:

MyCppCliClass::MyCppCliClass()
    : _my_dict(gcnew MyDictionary<MySimpleValueType, Something^>())
{...}

Now, if you like dependency injection, you will notice that this is bad. 现在,如果您喜欢依赖注入,您会发现这很糟糕。 Ideally, I should have a constructor like this: 理想情况下,我应该有一个像这样的构造函数:

MyCppClass::MyCppCliClass(IMyDictionary<MySimpleValueType, Something^>^ dict){...}

This class is instantiated in C#, so now my question: 这个类是用C#实例化的,所以现在我的问题是:

How can I instantiate this valid ref class that I am using right now outside of C++/CLI, given that (afaik) the C++ template typedef nor the pure C++ types are available in C#? 考虑到(afaik)C ++模板typedef或纯C ++类型在C#中可用,我如何实例化我现在正在C ++ / CLI之外使用的有效ref类?

MySimpleValueType obviously must be a native type of C#, or instantiation of the Dictionary will fail: MySimpleValueType显然必须是C#的本机类型,否则Dictionary的实例化将失败:

error C3225: generic type argument for 'T1' cannot be '...', it must be a value type
> or a handle to a reference type.

I feel I should be able to define the type in the C++/CLI class (with the typedef), but instantiate the Dictionary from outside. 我觉得我应该能够在C ++ / CLI类中定义类型(使用typedef),但是可以从外部实例化Dictionary。 C++/CLI typedefs are not available in C#, so maybe there is a way with a getter and type deduction of the var? C ++ / CLI typedef在C#中不可用,因此也许有一种使用getter和var进行类型推导的方法? Any ideas? 有任何想法吗?

boost is an unmanged library. boost是一个未管理的库。 That error message is telling you that you can't use an unmanaged type in a generic managed class. 该错误消息告诉您不能在通用托管类中使用非托管类型。

To work around this, make a managed class that will hold your unmanaged one, and use that in the container. 要解决此问题,请创建一个托管类,该类将容纳您的非托管类,并在容器中使用它。

public ref class MySimpleValueTypeHolder
{
private:
    MySimpleValueType* unmanaged;

public:
    property MySimpleValueType* ValueType { 
        MySimpleValueType* get() { return this->unmanaged; }
    }

    MySimpleValueTypeHolder() { this->unmanaged = new MySimpleValueType(); }
    ~MySimpleValueTypeHolder() { this->!MySimpleValueTypeHolder(); }

    !MySimpleValueTypeHolder()
    {
        if(this->unmanaged != nullptr)
        {
            delete this->unmanaged;
            this->unmanaged = nullptr;
        }
    }
};

Dictionary<MySimpleValueTypeHolder^, Something^>^ foo;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM