My test case is to check my own IsEqual method for TypeReference and
TypeDefintion equality.
class GenericType<T1,T2,T3>
{
public class NestedType
{
}
}
class User
{
public void UseNestedTypeT3()
{
new GenericType<int, int, string>.NestedType();
new GenericType<string, int, int>.NestedType();
}
}
[Test]
public void Compare_NestedTypeDef_With_NestedTypeUsage()
{
var n3 =
TypeQuery.GetTypeByName(TestConstants.BaseLibV1Assembly, typeNs +
".GenericType`3/NestedType");
TypeReference trn3 =
GetTypeRefFromMethod("UseNestedTypeT3",0);
TypeReference trn31 =
GetTypeRefFromMethod("UseNestedTypeT3",1);
Assert.IsTrue(n3.IsEqual(trn3, false));
...
The actual IsEqual method is defined like this:
public static bool IsEqual(this TypeReference x, TypeReference
y, bool bCompareGenericParameters)
{
if (x == null && y == null)
return true;
if ((x == null && y != null) ||
(x != null && y == null))
{
return false;
}
bool lret = false;
GenericInstanceType gtx = x as GenericInstanceType;
GenericInstanceType gty = y as GenericInstanceType;
TypeReference xDeclaring = x.DeclaringType;
TypeReference yDeclaring = y.DeclaringType;
xDeclaring.IsNotNull(() => xDeclaring =
x.DeclaringType.GetOriginalType());
yDeclaring.IsNotNull(() => yDeclaring =
y.DeclaringType.GetOriginalType());
// Generic parameters are passed as placeholder via method
reference
// newobj instance void class
[BaseLibraryV1]BaseLibrary.ApiChanges.PublicGenericClass`1<string>::.ctor(class
[System.Core]System.Func`1<!0>)
if (AreTypeNamesEqual(x.Name, y.Name) &&
x.Namespace == y.Namespace &&
IsEqual(xDeclaring, yDeclaring,
bCompareGenericParameters) &&
x.Scope.IsEqual(y.Scope))
{
if (bCompareGenericParameters)
{
lret =
x.GenericParameters.IsEqual(y.GenericParameters);
}
else
{
lret = true;
}
}
if (lret)
{
// Generics can have
GenericInstanceType xGen = x as GenericInstanceType;
GenericInstanceType yGen = y as GenericInstanceType;
if (xGen != null && yGen != null)
{
lret = xGen.IsEqual(yGen);
}
}
return lret;
}
On 16 Sep., 08:13, Jb Evain <[email protected]> wrote:
> Hey,
>
> On Thu, Sep 16, 2010 at 2:26 AM, Alois Kraus <[email protected]>
> wrote:
> > I just wanted to check if this a bug with Cecil 0.6 which has been
> > fixed with 0.9 or if this is by design. When I have a TypeReference to
> > a nested class it has the IsNested property set to false although in
> > its TypeDefinition it is set to true. Why is this?
>
> I don't know man, I don't know.
>
> It could very well be a bug!
>
> If you have a TypeRereference which represents a nested type, its
> DeclaringType property should point to, well, its declaring type you
> know. Thing is that, IsNested is implemented as:
>
> public bool IsNested {
> get { return this.DeclaringType != null; }
>
> }
>
> So if IsNested returns false, there must be something weird!
>
> In other words, I need a test-case.
>
> > How can I find out to which exact TypeDefinition is referred to
> > without actually resolving the TypeReference into a TypeDefinition?
>
> I'll have to admit that I'm not exactly sure what you're asking here.
>
> > I ask without because I do not want to load the dependant assemblies
> > for memory/perf reasons into memory.
>
> Sure thing!
>
> --
> Jb Evain <[email protected]>
--
--
mono-cecil