Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 472
G.Y Avatar asked Apr 23 '13 08:04

G.Y


1 Answers

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

Let's take a look on these implementations:

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;
  }
}

As you can see, IsValueTypeImpl() (which is called by IsValueType property) depends on inheritance AND IsClass depends on IsValueType (!).

Next, this description of ValueType states that it's not possible to inherit from ValueType directly.

So, the answer is no, you cannot create type which will be IsClass and IsValueType at the same time

like image 128
illegal-immigrant Avatar answered Nov 17 '22 15:11

illegal-immigrant