Add support for assembly custom attributes on modules (they are applied to a placeholder TypeRef record and compiler consuming the module is supposed to merge them into the assembly manifest).

This commit is contained in:
jfrijters 2010-11-29 11:20:47 +00:00
Родитель 89eb073f0f
Коммит 2db5fe38be
3 изменённых файлов: 76 добавлений и 0 удалений

Просмотреть файл

@ -1160,8 +1160,53 @@ namespace IKVM.Reflection.Emit
get { return asm.mdStreamVersion; }
}
private int AddTypeRefByName(int resolutionScope, string ns, string name)
{
TypeRefTable.Record rec = new TypeRefTable.Record();
rec.ResolutionScope = resolutionScope;
rec.TypeName = this.Strings.Add(name);
rec.TypeNameSpace = ns == null ? 0 : this.Strings.Add(ns);
return 0x01000000 | this.TypeRef.AddRecord(rec);
}
public void __Save(PortableExecutableKinds portableExecutableKind, ImageFileMachine imageFileMachine)
{
IList<CustomAttributeData> attributes = asm.GetCustomAttributesData(null);
if (attributes.Count > 0)
{
int mscorlib = ImportAssemblyRef(universe.Mscorlib);
int[] placeholderTokens = new int[4];
string[] placeholderTypeNames = new string[] { "AssemblyAttributesGoHere", "AssemblyAttributesGoHereM", "AssemblyAttributesGoHereS", "AssemblyAttributesGoHereSM" };
foreach (CustomAttributeData cad in attributes)
{
int index;
if (cad.Constructor.DeclaringType.BaseType == universe.System_Security_Permissions_CodeAccessSecurityAttribute)
{
if (cad.Constructor.DeclaringType.IsAllowMultipleCustomAttribute)
{
index = 3;
}
else
{
index = 2;
}
}
else if (cad.Constructor.DeclaringType.IsAllowMultipleCustomAttribute)
{
index = 1;
}
else
{
index = 0;
}
if (placeholderTokens[index] == 0)
{
// we manually add a TypeRef without looking it up in mscorlib, because Mono and Silverlight's mscorlib don't have these types
placeholderTokens[index] = AddTypeRefByName(mscorlib, "System.Runtime.CompilerServices", placeholderTypeNames[index]);
}
SetCustomAttribute(placeholderTokens[index], cad.__ToBuilder());
}
}
FillAssemblyRefTable();
ModuleWriter.WriteModule(null, null, this, PEFileKinds.Dll, portableExecutableKind, imageFileMachine, unmanagedResources, 0);
}

Просмотреть файл

@ -1495,6 +1495,25 @@ namespace IKVM.Reflection
return null;
}
internal bool IsAllowMultipleCustomAttribute
{
get
{
IList<CustomAttributeData> cad = GetCustomAttributesData(this.Module.universe.System_AttributeUsageAttribute);
if (cad.Count == 1)
{
foreach (CustomAttributeNamedArgument arg in cad[0].NamedArguments)
{
if (arg.MemberInfo.Name == "AllowMultiple")
{
return (bool)arg.TypedValue.Value;
}
}
}
return false;
}
}
internal bool IsPseudoCustomAttribute
{
get

Просмотреть файл

@ -110,6 +110,7 @@ namespace IKVM.Reflection
private Type typeof_System_Decimal;
private Type typeof_System_NonSerializedAttribute;
private Type typeof_System_SerializableAttribute;
private Type typeof_System_AttributeUsageAttribute;
private Type typeof_System_Reflection_AssemblyCultureAttribute;
private Type typeof_System_Runtime_InteropServices_DllImportAttribute;
private Type typeof_System_Runtime_InteropServices_FieldOffsetAttribute;
@ -133,6 +134,7 @@ namespace IKVM.Reflection
private Type typeof_System_Reflection_AssemblyTitleAttribute;
private Type typeof_System_Reflection_AssemblyInformationalVersionAttribute;
private Type typeof_System_Reflection_AssemblyFileVersionAttribute;
private Type typeof_System_Security_Permissions_CodeAccessSecurityAttribute;
private Type typeof_System_Security_Permissions_HostProtectionAttribute;
private Type typeof_System_Security_Permissions_PermissionSetAttribute;
private Type typeof_System_Security_Permissions_SecurityAction;
@ -283,6 +285,11 @@ namespace IKVM.Reflection
get { return typeof_System_SerializableAttribute ?? (typeof_System_SerializableAttribute = ImportMscorlibType(typeof(System.SerializableAttribute))); }
}
internal Type System_AttributeUsageAttribute
{
get { return typeof_System_AttributeUsageAttribute ?? (typeof_System_AttributeUsageAttribute = ImportMscorlibType(typeof(System.AttributeUsageAttribute))); }
}
internal Type System_Reflection_AssemblyCultureAttribute
{
get { return typeof_System_Reflection_AssemblyCultureAttribute ?? (typeof_System_Reflection_AssemblyCultureAttribute = ImportMscorlibType(typeof(System.Reflection.AssemblyCultureAttribute))); }
@ -398,6 +405,11 @@ namespace IKVM.Reflection
get { return typeof_System_Reflection_AssemblyFileVersionAttribute ?? (typeof_System_Reflection_AssemblyFileVersionAttribute = ImportMscorlibType(typeof(System.Reflection.AssemblyFileVersionAttribute))); }
}
internal Type System_Security_Permissions_CodeAccessSecurityAttribute
{
get { return typeof_System_Security_Permissions_CodeAccessSecurityAttribute ?? (typeof_System_Security_Permissions_CodeAccessSecurityAttribute = ImportMscorlibType(typeof(System.Security.Permissions.CodeAccessSecurityAttribute))); }
}
internal Type System_Security_Permissions_HostProtectionAttribute
{
get { return typeof_System_Security_Permissions_HostProtectionAttribute ?? (typeof_System_Security_Permissions_HostProtectionAttribute = ImportMscorlibType(typeof(System.Security.Permissions.HostProtectionAttribute))); }