Skip to content
/ cecil Public
forked from jbevain/cecil
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for generic attributes #36

Merged
merged 2 commits into from
Aug 23, 2022
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
7 changes: 5 additions & 2 deletions Mono.Cecil/AssemblyReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3513,9 +3513,12 @@ public void ReadCustomAttributeConstructorArguments (CustomAttribute attribute,

attribute.arguments = new Collection<CustomAttributeArgument> (count);

for (int i = 0; i < count; i++)
for (int i = 0; i < count; i++) {
var parameterType = GenericParameterResolver.ResolveParameterTypeIfNeeded (
attribute.Constructor, parameters [i]);
attribute.arguments.Add (
ReadCustomAttributeFixedArgument (parameters [i].ParameterType));
ReadCustomAttributeFixedArgument (parameterType));
}
}

CustomAttributeArgument ReadCustomAttributeFixedArgument (TypeReference type)
Expand Down
7 changes: 5 additions & 2 deletions Mono.Cecil/AssemblyWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2928,8 +2928,11 @@ public void WriteCustomAttributeConstructorArguments (CustomAttribute attribute)
if (parameters.Count != arguments.Count)
throw new InvalidOperationException ();

for (int i = 0; i < arguments.Count; i++)
WriteCustomAttributeFixedArgument (parameters [i].ParameterType, arguments [i]);
for (int i = 0; i < arguments.Count; i++) {
var parameterType = GenericParameterResolver.ResolveParameterTypeIfNeeded (
attribute.Constructor, parameters [i]);
WriteCustomAttributeFixedArgument (parameterType, arguments [i]);
}
}

void WriteCustomAttributeFixedArgument (TypeReference type, CustomAttributeArgument argument)
Expand Down
74 changes: 74 additions & 0 deletions Test/Mono.Cecil.Tests/CustomAttributesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,80 @@ public void GenericParameterConstraint ()
}, verify: !Platform.OnMono);
}

[Test]
public void GenericAttributeString ()
{
TestModule ("GenericAttributes.dll", module => {
var type = module.GetType ("WithGenericAttribute_OfString");
Assert.IsTrue (type.HasCustomAttributes);
var attributes = type.CustomAttributes;
Assert.AreEqual (1, attributes.Count);
Assert.AreEqual ("GenericAttribute`1<System.String>", attributes [0].AttributeType.FullName);
var attribute = attributes [0];
// constructor arguments
Assert.AreEqual (true, attribute.HasConstructorArguments);
var argument = attribute.ConstructorArguments.Single ();
Assert.AreEqual ("System.String", argument.Type.FullName);
Assert.AreEqual ("t", argument.Value);
// named field argument
Assert.AreEqual (true, attribute.HasFields);
var field = attribute.Fields.Single ();
Assert.AreEqual ("F", field.Name);
Assert.AreEqual ("System.String", field.Argument.Type.FullName);
Assert.AreEqual ("f", field.Argument.Value);
// named property argument
Assert.AreEqual (true, attribute.HasProperties);
var property = attribute.Properties.Single ();
Assert.AreEqual ("P", property.Name);
Assert.AreEqual ("System.String", property.Argument.Type.FullName);
Assert.AreEqual ("p", property.Argument.Value);

}, verify: !Platform.OnMono);
}

[Test]
public void GenericAttributeInt ()
{
TestModule ("GenericAttributes.dll", module => {
var type = module.GetType ("WithGenericAttribute_OfInt");
Assert.IsTrue (type.HasCustomAttributes);
var attributes = type.CustomAttributes;
Assert.AreEqual (1, attributes.Count);
Assert.AreEqual ("GenericAttribute`1<System.Int32>", attributes [0].AttributeType.FullName);
var attribute = attributes [0];
// constructor arguments
Assert.AreEqual (true, attribute.HasConstructorArguments);
var argument = attribute.ConstructorArguments.Single ();
Assert.AreEqual ("System.Int32", argument.Type.FullName);
Assert.AreEqual (1, argument.Value);
// named field argument
Assert.AreEqual (true, attribute.HasFields);
var field = attribute.Fields.Single ();
Assert.AreEqual ("F", field.Name);
Assert.AreEqual ("System.Int32", field.Argument.Type.FullName);
Assert.AreEqual (2, field.Argument.Value);
// named property argument
Assert.AreEqual (true, attribute.HasProperties);
var property = attribute.Properties.Single ();
Assert.AreEqual ("P", property.Name);
Assert.AreEqual ("System.Int32", property.Argument.Type.FullName);
Assert.AreEqual (3, property.Argument.Value);
}, verify: !Platform.OnMono);
}

[Test]
public void ConstrainedGenericAttribute ()
{
TestModule ("GenericAttributes.dll", module => {
var type = module.GetType ("WithConstrainedGenericAttribute");
Assert.IsTrue (type.HasCustomAttributes);
var attributes = type.CustomAttributes;
Assert.AreEqual (1, attributes.Count);
var attribute = attributes [0];
Assert.AreEqual ("ConstrainedGenericAttribute`1<DerivedFromConstraintType>", attribute.AttributeType.FullName);
}, verify: !Platform.OnMono);
}

[Test]
public void NullCharInString ()
{
Expand Down
Binary file not shown.