Type Overflow...
-
I have an interface and a type implementing it:
public interface IValueWithUnit {...}
[Serializable]
public struct DoubleValueWithUnit : IValueWithUnit { ... }At a different place, I have a
string
with the name of the type, and must find out if the type derives from above interface:string name = "DoubleValueWithUnit";
Type t;
if (TypeHelper.TryFindType(name, out t))
{
m_IsValueWithUnit = t.IsSubclassOf(typeof(IValueWithUnit)); // false
m_IsValueWithUnit = t.IsSubclassOf(typeof(DoubleValueWithUnit)); // false
m_IsValueWithUnit = t.IsInstanceOfType(typeof(IValueWithUnit)); // false
m_IsValueWithUnit = t.IsInstanceOfType(typeof(DoubleValueWithUnit)); // false
m_IsValueWithUnit = t.IsAssignableFrom(typeof(IValueWithUnit)); // false
m_IsValueWithUnit = t.IsAssignableFrom(typeof(DoubleValueWithUnit)); // true
m_IsValueWithUnit = t.GetInterface(nameof(IValueWithUnit)) != null; // trueThe
TryFindType
function is from c# - Type.GetType("namespace.a.b.ClassName") returns null - Stack Overflow[^]. You see the many "false" comments?DoubleValueWithUnit
is neither a subclass nor an instance ofDoubleValueWithUnit
orIValueWithUnit
, and is also not assignable fromIValueWithUnit
, but at least it is assignable fromDoubleValueWithUnit
. I am confused now: my brain threw aTypeOverflowException
.Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!
-
I have an interface and a type implementing it:
public interface IValueWithUnit {...}
[Serializable]
public struct DoubleValueWithUnit : IValueWithUnit { ... }At a different place, I have a
string
with the name of the type, and must find out if the type derives from above interface:string name = "DoubleValueWithUnit";
Type t;
if (TypeHelper.TryFindType(name, out t))
{
m_IsValueWithUnit = t.IsSubclassOf(typeof(IValueWithUnit)); // false
m_IsValueWithUnit = t.IsSubclassOf(typeof(DoubleValueWithUnit)); // false
m_IsValueWithUnit = t.IsInstanceOfType(typeof(IValueWithUnit)); // false
m_IsValueWithUnit = t.IsInstanceOfType(typeof(DoubleValueWithUnit)); // false
m_IsValueWithUnit = t.IsAssignableFrom(typeof(IValueWithUnit)); // false
m_IsValueWithUnit = t.IsAssignableFrom(typeof(DoubleValueWithUnit)); // true
m_IsValueWithUnit = t.GetInterface(nameof(IValueWithUnit)) != null; // trueThe
TryFindType
function is from c# - Type.GetType("namespace.a.b.ClassName") returns null - Stack Overflow[^]. You see the many "false" comments?DoubleValueWithUnit
is neither a subclass nor an instance ofDoubleValueWithUnit
orIValueWithUnit
, and is also not assignable fromIValueWithUnit
, but at least it is assignable fromDoubleValueWithUnit
. I am confused now: my brain threw aTypeOverflowException
.Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!
What is the relation between the
name
variable, and them_Setting.Type
property? Here's what I can understand from the results you get:DoubleValueWithUnit
is not a subclass ofIValueWithUnit
. An interface implementation is not considered as inheritance.DoubleValueWithUnit
is not a subclass ofDoubleValueWithUnit
. It is the very same class.DoubleValueWithUnit
is not an instance ofIValueWithUnit
. It is the type itself, not an instance of the type.DoubleValueWithUnit
is not an instance ofDoubleValueWithUnit
. Same comment as above.DoubleValueWithUnit
is not assignable fromIValueWithUnit
. It implements the interface, but that does not mean that every implementation of the interface should allow to cast to actual type. There could be some properties/methods of the class which are not part of the interface.DoubleValueWithUnit
is assignable fromDoubleValueWithUnit
. This one seems logical.DoubleValueWithUnit
implementsIValueWithUnit
. ThusIValueWithUnit
is an element of the list of interfaces implemented byDoubleValueWithUnit
.
"Five fruits and vegetables a day? What a joke! Personally, after the third watermelon, I'm full."
-
What is the relation between the
name
variable, and them_Setting.Type
property? Here's what I can understand from the results you get:DoubleValueWithUnit
is not a subclass ofIValueWithUnit
. An interface implementation is not considered as inheritance.DoubleValueWithUnit
is not a subclass ofDoubleValueWithUnit
. It is the very same class.DoubleValueWithUnit
is not an instance ofIValueWithUnit
. It is the type itself, not an instance of the type.DoubleValueWithUnit
is not an instance ofDoubleValueWithUnit
. Same comment as above.DoubleValueWithUnit
is not assignable fromIValueWithUnit
. It implements the interface, but that does not mean that every implementation of the interface should allow to cast to actual type. There could be some properties/methods of the class which are not part of the interface.DoubleValueWithUnit
is assignable fromDoubleValueWithUnit
. This one seems logical.DoubleValueWithUnit
implementsIValueWithUnit
. ThusIValueWithUnit
is an element of the list of interfaces implemented byDoubleValueWithUnit
.
"Five fruits and vegetables a day? What a joke! Personally, after the third watermelon, I'm full."
phil.o wrote:
What is the relation between the
name
variable, and them_Setting.Type
property?Oh, I forgot to replace that; question updated. Thanks for your explanation!
Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!
-
I have an interface and a type implementing it:
public interface IValueWithUnit {...}
[Serializable]
public struct DoubleValueWithUnit : IValueWithUnit { ... }At a different place, I have a
string
with the name of the type, and must find out if the type derives from above interface:string name = "DoubleValueWithUnit";
Type t;
if (TypeHelper.TryFindType(name, out t))
{
m_IsValueWithUnit = t.IsSubclassOf(typeof(IValueWithUnit)); // false
m_IsValueWithUnit = t.IsSubclassOf(typeof(DoubleValueWithUnit)); // false
m_IsValueWithUnit = t.IsInstanceOfType(typeof(IValueWithUnit)); // false
m_IsValueWithUnit = t.IsInstanceOfType(typeof(DoubleValueWithUnit)); // false
m_IsValueWithUnit = t.IsAssignableFrom(typeof(IValueWithUnit)); // false
m_IsValueWithUnit = t.IsAssignableFrom(typeof(DoubleValueWithUnit)); // true
m_IsValueWithUnit = t.GetInterface(nameof(IValueWithUnit)) != null; // trueThe
TryFindType
function is from c# - Type.GetType("namespace.a.b.ClassName") returns null - Stack Overflow[^]. You see the many "false" comments?DoubleValueWithUnit
is neither a subclass nor an instance ofDoubleValueWithUnit
orIValueWithUnit
, and is also not assignable fromIValueWithUnit
, but at least it is assignable fromDoubleValueWithUnit
. I am confused now: my brain threw aTypeOverflowException
.Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!
As Phil explained, you've got the
IsAssignableFrom
the wrong way round:m_IsValueWithUnit = typeof(IValueWithUnit).IsAssignableFrom(t); // true
But rest assured you're not alone in being confused by that method. :) Nick Craver on Twitter: "Anyone else pretty reliably get .IsAssignableFrom() backwards?"[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
As Phil explained, you've got the
IsAssignableFrom
the wrong way round:m_IsValueWithUnit = typeof(IValueWithUnit).IsAssignableFrom(t); // true
But rest assured you're not alone in being confused by that method. :) Nick Craver on Twitter: "Anyone else pretty reliably get .IsAssignableFrom() backwards?"[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Thanks. Good to know that.
Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!