While doing something almost completely irrelevant, a question popped into my head:
Can an expression of the form obj.GetType().IsInterface
ever be true in a codebase consisting exclusively of C# code?
I suspect the answer is no, because:
GetType()
will always return the runtime type.[ComImport, CoClass(typeof(MyClass))]
on an interface makes it look like you can instantiate it, but the constructor call actually instantiates the referenced class.I can't think of any other case. Am I missing something, or is my guess correct?
Can an expression of the form
obj.GetType().IsInterface
ever be true in a codebase consisting exclusively of C# code?
Yes - but probably not the way you were thinking of:
using System;
public class EvilClass
{
public new Type GetType()
{
return typeof(IDisposable);
}
}
class Test
{
static void Main()
{
EvilClass obj = new EvilClass();
Console.WriteLine(obj.GetType().IsInterface); // True
}
}
Slightly similarly, I believe you could create a subclass of RealProxy
which would intercept the call and return an interface type.
If you mean "will the return value of the GetType()
method declared in object
ever be an interface type" - in that case I suspect the answer is no.
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