【问题标题】:How do I make a variable that requires a type of a class that inherits an abstract?如何创建需要继承抽象类的类型的变量?
【发布时间】:2018-05-15 05:39:24
【问题描述】:

我希望一个变量只包含继承特定抽象的类类型。在此示例中,变量“MyLetter”应该只包含类型 LetterA、LetterB 或任何继承 LetterAbstract 的类型。

我将如何做到这一点?

abstract class LetterAbstract
{
    public char Letter;
}

class LetterA : LetterAbstract
{
    void LetterA()
    {
        Letter = 'A';
    }
}

// valid
TLetterAbstract MyLetter = typeof(LetterA);

// invalid
TLetterAbstract MyLetter = typeof(string);

【问题讨论】:

  • "A"[0]'A'不一样吗?
  • 你应该如何定义字符?很高兴知道。我之前只用过"A"[0]
  • 顺便说一句,您上面的代码 sn-p not 编译。另外,您不会在任何地方定义TLetterAbstract
  • 我想泛型是你需要的。具有约束where T: LetterAbstract 的泛型类型正是您所描述的。

标签: c# inheritance types


【解决方案1】:

在这种情况下,您需要使用数据类型LetterAbstract(所谓的基类)声明变量。

LetterAbstract letter;

letter = new LetterA(); // valid
letter = "asdasd"; // invalid

我认为您需要先阅读有关继承的内容,请参阅“Inheritance (C# Programming Guide)”。

:编辑

关于

我不想要一个 LetterA 的实例。我想要类类型 LetterA。

你可以用泛型来摆弄一些东西,但我不知道用例可能是什么:

class TypeVar<A>
  where A: LetterAbstract
{
  public void SetValue<B>()
    where B: A
  {
    mValue = typeof(B);
  }

  public System.Type GetValue()
  {
    return mValue;
  }

  private System.Type mValue;
}

var x =new TypeVar<LetterAbstract>();
x.SetValue<LetterA>(); // valid
x.SetValue<string>(); // invalid
x.GetValue(); // get the type

【讨论】:

  • 我不想要一个 LetterA 的实例。我想要类类型 LetterA。
  • @Josh 您似乎不清楚一方面是什么类型、类,另一方面是实例和对象。您应该首先了解这一点。你的评论毫无意义。
【解决方案2】:

我不想要一个 LetterA 的实例。我想要类类型 LetterA

虽然你想要什么对我来说似乎并不明确。试试这个。

请注意,如果您想限制您的编码,那么除非您声明一个变量Letter,否则这是不可能的,但您可以确定一个变量是否是某种类型的子类。

static bool IsLetter(object obj)
{
    Type type = obj.GetType();

    return type.IsSubclassOf(typeof(Letter)) || type == typeof(Letter);
}

示例:

class Foo
{

}

abstract class Letter
{

}

class LetterA : Letter
{

}

class LetterAA : LetterA
{

}

class LetterAB : LetterA
{

}

用法:

LetterAA aa = new LetterAA();
LetterAB ab = new LetterAB();
Foo foo = new Foo();

bool isAA = IsLetter(aa);
bool isAB = IsLetter(ab);
bool isLetter = IsLetter(foo);

Console.WriteLine(isAA); // True
Console.WriteLine(isAB); // True
Console.WriteLine(isLetter); // False

【讨论】:

    【解决方案3】:

    你想过TLetterAbstract 类吗?你可以这样定义它:

    public class TLetterAbstract : Type
    {
        private Type _T;
        public TLetterAbstract(object o)
        {
            if (o is LetterAbstract)
            {
                _T = o.GetType();
            }
            throw new ArgumentException("Wrong input type");
        }
        public TLetterAbstract(Type t)
        {
            if (t.IsInstanceOfType(typeof(LetterAbstract)))
            {
                _T = t;
            }
            throw new ArgumentException("Wrong input type");
        }
    
        public override object[] GetCustomAttributes(bool inherit)
        {
            return _T.GetCustomAttributes(inherit);
        }
        // and some more abstract members of Type implemented by simply forwarding to the instance
    }
    

    然后你必须改变你的代码

    TLetterAbstract MyLetter = typeof(LetterA);
    

    TLetterAbstract MyLetter = new TLetterAbstract(typeof(LetterA));
    

    【讨论】:

      猜你喜欢
      • 2019-12-05
      • 2023-01-19
      • 2018-03-07
      • 2011-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-05
      • 1970-01-01
      相关资源
      最近更新 更多