J
john
Hi to All
To demonstrate:
public class MyBaseGenericClass<T>
{
}
public class MyGenericClass1<T> : MyBaseGenericClass<T>
{
}
public class MyGenericClass2<T> : MyBaseGenericClass<T>
{
}
public class Test
{
public void Method()
{
Type baseGenericType = typeof (MyBaseGenericClass<>);
Type genericType1 = typeof(MyGenericClass1<>);
Type genericType2 = typeof(MyGenericClass2<>);
// .Equals gives the same results (correctly), so that's not the trick
bool b1 = genericType1.BaseType == baseGenericType; // False, why?
bool b2 = genericType2.BaseType == baseGenericType; // False, why?
bool b3 = genericType1.BaseType == genericType2.BaseType; // False, why?
// Some diag info:
DumpType(baseGenericType);
DumpType(genericType1.BaseType);
DumpType(genericType2.BaseType);
}
private void DumpType(Type type)
{
Debug.WriteLine(string.Format("Type.Name:{0}, Type.FullName: {1},
Type.GUID: {2}", type.Name, type.FullName, type.GUID ));
}
}
Diag output:
Type.Name:MyBaseGenericClass`1, Type.FullName:
WindowsApplication25.MyBaseGenericClass`1, Type.GUID:
807bb6c8-35ad-3408-8dfd-13eb15f39a0a
Type.Name:MyBaseGenericClass`1, Type.FullName: , Type.GUID:
807bb6c8-35ad-3408-8dfd-13eb15f39a0a
Type.Name:MyBaseGenericClass`1, Type.FullName: , Type.GUID:
807bb6c8-35ad-3408-8dfd-13eb15f39a0a
As it seems all three types are should be (and logically are) identical, but
we detect all three as different.
NOTE: All the three types Name and GUID is identical.
The first type what we got with typeof operator has FullName, but second and
third has not.
But not this is the fact what makes the equality operator fail. Type
instances as a convention shoud be singleton by Type what it describes, so
there is no more than one type instance by Type. As a consequence the
polymorphic overload of Equals method of Type only uses the simple reference
comparision.
(Well not exactly, it compares the UnderlyingTpye but this is the same in
our case)
It is clear as a diagnostic result there are three instances and not one
singleton.
As a consequence of erratic result of comparison some of the internal
algorithmi also works with error for example: IsSubTypeOf()
To wrap up: Both implementaion, both the behavior of this is erratic.
Again, the three GIUDs are identical.
Any ideas?
To demonstrate:
public class MyBaseGenericClass<T>
{
}
public class MyGenericClass1<T> : MyBaseGenericClass<T>
{
}
public class MyGenericClass2<T> : MyBaseGenericClass<T>
{
}
public class Test
{
public void Method()
{
Type baseGenericType = typeof (MyBaseGenericClass<>);
Type genericType1 = typeof(MyGenericClass1<>);
Type genericType2 = typeof(MyGenericClass2<>);
// .Equals gives the same results (correctly), so that's not the trick
bool b1 = genericType1.BaseType == baseGenericType; // False, why?
bool b2 = genericType2.BaseType == baseGenericType; // False, why?
bool b3 = genericType1.BaseType == genericType2.BaseType; // False, why?
// Some diag info:
DumpType(baseGenericType);
DumpType(genericType1.BaseType);
DumpType(genericType2.BaseType);
}
private void DumpType(Type type)
{
Debug.WriteLine(string.Format("Type.Name:{0}, Type.FullName: {1},
Type.GUID: {2}", type.Name, type.FullName, type.GUID ));
}
}
Diag output:
Type.Name:MyBaseGenericClass`1, Type.FullName:
WindowsApplication25.MyBaseGenericClass`1, Type.GUID:
807bb6c8-35ad-3408-8dfd-13eb15f39a0a
Type.Name:MyBaseGenericClass`1, Type.FullName: , Type.GUID:
807bb6c8-35ad-3408-8dfd-13eb15f39a0a
Type.Name:MyBaseGenericClass`1, Type.FullName: , Type.GUID:
807bb6c8-35ad-3408-8dfd-13eb15f39a0a
As it seems all three types are should be (and logically are) identical, but
we detect all three as different.
NOTE: All the three types Name and GUID is identical.
The first type what we got with typeof operator has FullName, but second and
third has not.
But not this is the fact what makes the equality operator fail. Type
instances as a convention shoud be singleton by Type what it describes, so
there is no more than one type instance by Type. As a consequence the
polymorphic overload of Equals method of Type only uses the simple reference
comparision.
(Well not exactly, it compares the UnderlyingTpye but this is the same in
our case)
It is clear as a diagnostic result there are three instances and not one
singleton.
As a consequence of erratic result of comparison some of the internal
algorithmi also works with error for example: IsSubTypeOf()
To wrap up: Both implementaion, both the behavior of this is erratic.
Again, the three GIUDs are identical.
Any ideas?