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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With