9

Is it possible to design a C# class that when querying it through reflections will mark itself as positive IsValueType and positive IsClass?

Or are they actually mutually exclusive markings?

I know that,

Most primitive types will return (including enums & structs):
IsValueType=true, IsClass=false.

String or any class - abstracts too.. return:
IsValueType=false, IsClass=true.

Interfaces returns:
IsValueType=false, IsClass=false

4

1 回答 1

7

是否可以设计一个 C# 类,当通过反射查询它时会将其自身标记为正 IsValueType 和正 IsClass?

让我们看一下这些实现:

protected virtual bool IsValueTypeImpl()
{
      return this.IsSubclassOf((Type) RuntimeType.ValueType);
}

public bool IsClass
{
  [__DynamicallyInvokable] get
  {
    if ((this.GetAttributeFlagsImpl() & TypeAttributes.ClassSemanticsMask) == TypeAttributes.NotPublic)
      return !this.IsValueType;
    else
      return false;
  }
}

如您所见,IsValueTypeImpl()(由IsValueType属性调用)依赖于继承并且IsClass 依赖于 IsValueType(!)。

接下来,这个 ValueType 的描述表明不能直接从 ValueType 继承

所以,答案是否定的,你不能同时创建IsClass类型IsValueType

于 2013-04-23T08:49:38.440 回答