Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion Mono.Cecil/AssemblyWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2988,7 +2988,7 @@ void WriteCustomAttributeValue (TypeReference type, object value)
break;
case ElementType.None:
if (type.IsTypeOf ("System", "Type"))
WriteTypeReference ((TypeReference) value);
WriteCustomAttributeTypeValue ((TypeReference) value);
else
WriteCustomAttributeEnumValue (type, value);
break;
Expand All @@ -2998,6 +2998,27 @@ void WriteCustomAttributeValue (TypeReference type, object value)
}
}

private void WriteCustomAttributeTypeValue (TypeReference value)
{
var typeDefinition = value as TypeDefinition;

if (typeDefinition != null) {
TypeDefinition outermostDeclaringType = typeDefinition;
while (outermostDeclaringType.DeclaringType != null)
outermostDeclaringType = outermostDeclaringType.DeclaringType;

// In CLR .winmd files, custom attribute arguments reference unmangled type names (rather than <CLR>Name)
if (WindowsRuntimeProjections.IsClrImplementationType (outermostDeclaringType)) {
WindowsRuntimeProjections.Project (outermostDeclaringType);
WriteTypeReference (value);
WindowsRuntimeProjections.RemoveProjection (outermostDeclaringType);
return;
}
}

WriteTypeReference (value);
}

void WritePrimitiveValue (object value)
{
if (value == null)
Expand Down
2 changes: 1 addition & 1 deletion Mono.Cecil/WindowsRuntimeProjections.cs
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ static bool NeedsWindowsRuntimePrefix (TypeDefinition type)
return true;
}

static bool IsClrImplementationType (TypeDefinition type)
public static bool IsClrImplementationType (TypeDefinition type)
{
if ((type.Attributes & (TypeAttributes.VisibilityMask | TypeAttributes.SpecialName)) != TypeAttributes.SpecialName)
return false;
Expand Down
2 changes: 1 addition & 1 deletion Test/Mono.Cecil.Tests/TypeParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public void ByRefTypeReference ()
public void FullyQualifiedTypeReference ()
{
var module = GetCurrentModule ();
var cecil = module.AssemblyReferences.Where (reference => reference.Name == "Mono.Cecil").First ();
var cecil = module.AssemblyReferences.Where (reference => reference.Name != typeof (TypeDefinition).Assembly.GetName ().Name).First ();

var fullname = "Mono.Cecil.TypeDefinition, " + cecil.FullName;

Expand Down
18 changes: 18 additions & 0 deletions Test/Mono.Cecil.Tests/WindowsRuntimeProjectionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,24 @@ public void CanProjectClasses ()
Assert.AreEqual (TypeDefinitionTreatment.PrefixWindowsRuntimeName, winrtSomeOtherClassType.WindowsRuntimeProjection.Treatment);
}, verify: false, assemblyResolver: WindowsRuntimeAssemblyResolver.CreateInstance (), applyWindowsRuntimeProjections: true);
}

[Test]
public void VerifyTypeReferenceToProjectedTypeInAttributeArgumentReferencesUnmangledTypeName()
{
if (Platform.OnMono)
return;

TestModule(ModuleName, (module) =>
{
var type = module.Types.Single(t => t.Name == "ClassWithAsyncMethod");
var method = type.Methods.Single(m => m.Name == "DoStuffAsync");

var attribute = method.CustomAttributes.Single(a => a.AttributeType.Name == "AsyncStateMachineAttribute");
var attributeArgument = (TypeReference)attribute.ConstructorArguments[0].Value;

Assert.AreEqual("ManagedWinmd.ClassWithAsyncMethod/<DoStuffAsync>d__0", attributeArgument.FullName);
}, verify: false, assemblyResolver: WindowsRuntimeAssemblyResolver.CreateInstance(), applyWindowsRuntimeProjections: true);
}
}

[TestFixture]
Expand Down
Binary file modified Test/Resources/assemblies/ManagedWinmd.winmd
Binary file not shown.