如何创建相关类型的组在C#泛型使用给定的,有没有`typedef`? [英] How do create groups of related types for use in c# generics given that there's no `typedef`?

查看:205
本文介绍了如何创建相关类型的组在C#泛型使用给定的,有没有`typedef`?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为一个谁来自C ++背景,我遇到以下情况:

As one who comes from a C++ background, I've come across the following situation:

由于C#不支持的typedef,你怎么编程相关类型。这是在C ++说,我可以存储与类型,类型定义检索与模板的使用时。同样的事情也不会在C#来完成同样的方式,因为缺乏的typedef

Given that c# does not support typedefs, how do you programmatically relate types. That is to say in C++ I can store related types as typedefs for retrieval when used with templates. The same thing cannot be done in c# in the same way because of the lack of typedef.

例如在C ++我想:

template< typename T >
class Thing : public ThingBase<T>
{
   public:
       bool isRelated( T::Parent & otherThing )
       {
           T::Auxillary aux( "Yes" );            
           return aux.isValid( otherThing ) && data.isParent( otherThing );
       }
   private:
       T data;
};

这将与下面的任一工作:

That would work with either of the following:

class SomeClass
{
    public:
        typedef SomeClassParent Parent; 
        typedef FooAuxillary    Auxillary;

        bool isParent( Parent & parentObject );
};

class OtherClass
{
    public:
        typedef OtherClassParent Parent; 
        typedef BarAuxillary     Auxillary;

        bool isParent( Parent & parentObject );
};



虽然能够调用 T :: isParent 参数类型可以用一个通用的接口界面来实现,建设 ::事情的isRelated签署好像这将是没有类型定义是不可能的。

While the being able to call T::isParent on the parameter type could be achieved with a common interface interface, building the signature for Thing::isRelated seems like it would be impossible without typedefs.

所以,在C#中我该怎么办得到(在C ++例子是T ::接口)T的相关接口类型?

So, in C# what do I do to get the related interface type for T (which is T::Interface in the C++ example)?

推荐答案

您需要使用通用的约束(以及可能在这种情况下)第二泛型参数的指示SLaks。

You need to use Generic constraints (and likely in this case) a second generic parameter as SLaks as indicated.

interface IRelated<TRelated>
{
    bool isParent(TRelated parentObject);
}

class SomeClassParent {}
class SomeClass : IRelated<SomeClassParent>
{
    bool isParent(SomeClassParent parentObject)
    {
        // your logic here
        return true;
    }
}

class Thing<T, TParent> : ThingBase<T> where T : IRelated<TParent> {}

要使用:

var obj = new Thing<SomeClass, SomeClassParent>();

下面是另一种选择。这其中需要一些运行时类型检查(并允许isParent与任何类型的对象调用),但它清理语法。

Here is another option. This one requires some runtime type checking (and allows isParent to be called with an object of any type), but it cleans up the syntax.

interface IRelated
{
    bool isParent<T>(T parentObject);
}

class SomeClassParent {}
class SomeClass : IRelated
{
    bool isParent<T>(T parentObject)
    {
        if (parentObject is SomeClassParent)
        {
            // your logic here
            return true;
        }
    }
}

class Thing<T> : ThingBase<T> where T : IRelated {}

要使用:

var obj = new Thing<SomeClass>();

这篇关于如何创建相关类型的组在C#泛型使用给定的,有没有`typedef`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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