使用C ++ CLI模板类在C# [英] Use C++ CLI template class in C#

查看:163
本文介绍了使用C ++ CLI模板类在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的类在C ++ / CLI和明确模板实例为INT原始..

I have the following class in C++/CLI and an explicit template instantiation for the int primitive..

template<typename T>
public ref class Number
{
    T _value;
public:
    static property T MinValue
    {
        T get()
        {
            return T::MinValue;
        }
    }

    static property T MaxValue
    {
        T get()
        {
            return T::MaxValue;
        }
    }

    property T Value
    {
        T get()
        {
            return _value;
        }

        void set(T value)
        {
            if( value<MinValue || value > MaxValue)
                throw gcnew System::ArgumentException("Value out of range");

            _value = value;
        }
    }

};

template ref class Number<int>;

在编写本和生成的程序集使用反射镜检查,我能看到一类名为数字与LT; INT&GT; 而是试图在C#编译器实例化这个相同的类抱怨一些系统::数类没有采取一个模板参数。我究竟做错了什么?可这都做些什么呢?

On compiling this and inspecting the generated assembly using reflector I am able to see a class called Number<int> but while trying to instantiate this same class in C# the compiler complains about some System::Number class not taking a template argument. What am I doing wrong? Can this be done at all?

推荐答案

我身边有一个工作,申报附加的类继承了数字与LT; INT&GT; 类。这个类是目前在C#中可见,并可被实例化。

I have a work around, declare an additional class inheriting the Number<int> class. This class is now visible in C# and can be instantiated.

public ref class MyInt32 : public Number<int>
{
};

这篇关于使用C ++ CLI模板类在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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