in ILRepack/PermissionsetHelper.cs [98:151]
public static SecurityDeclaration Xml2PermissionSet(SecurityDeclaration xmlDeclaration, ModuleDefinition targetModule)
{
if (!xmlDeclaration.HasSecurityAttributes || xmlDeclaration.SecurityAttributes.Count == 0)
// nothing to convert
return null;
if (xmlDeclaration.SecurityAttributes.Count > 1)
throw new Exception("Cannot convert SecurityDeclaration with more than one attribute");
SecurityAttribute sa = xmlDeclaration.SecurityAttributes[0];
if (sa.HasFields)
throw new NotSupportedException("Cannot convert SecurityDeclaration with fields");
if (!sa.HasProperties || sa.Properties.Count > 1)
throw new NotSupportedException("Invalid XML SecurityDeclaration (only 1 property supported)");
CustomAttributeNamedArgument arg = sa.Properties[0];
if (arg.Name != "XML" || arg.Argument.Type.FullName != "System.String")
throw new ArgumentException("Property \"XML\" expected");
if (string.IsNullOrEmpty(arg.Argument.Value as string))
return null;
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml((string)arg.Argument.Value);
XmlNode permissionSet = xmlDoc.SelectSingleNode("/PermissionSet");
if (permissionSet == null)
return null;
XmlNode permissionSetClass = permissionSet.SelectSingleNode("@class"); // check version?
if (permissionSetClass == null)
return null;
if (permissionSetClass.Value != "System.Security.PermissionSet")
return null;
XmlNode iPermission = permissionSet.SelectSingleNode("IPermission");
if (iPermission == null)
return null;
XmlNode iPermissionClass = iPermission.SelectSingleNode("@class"); // check version?
if (iPermissionClass == null)
return null;
// Create Namespace & Name from FullName, AssemblyName can be ignored since we look it up.
string[] valueParts = iPermissionClass.Value.Split(',');
Collection<string> classNamespace = new Collection<string>(valueParts[0].Split('.'));
string assemblyName = valueParts[1].Trim();
string className = classNamespace[classNamespace.Count - 1];
classNamespace.RemoveAt(classNamespace.Count - 1);
SecurityAttribute attribute = new SecurityAttribute(GetTypeRef(string.Join(".", classNamespace.ToArray()), className, assemblyName, targetModule));
foreach (XmlAttribute xmlAttr in iPermission.Attributes)
{
if ((xmlAttr.Name != "class") && (xmlAttr.Name != "version"))
{
attribute.Properties.Add(new CustomAttributeNamedArgument(xmlAttr.Name,
new CustomAttributeArgument(targetModule.TypeSystem.String, xmlAttr.Value)));
}
}
SecurityDeclaration newSd = new SecurityDeclaration(xmlDeclaration.Action);
newSd.SecurityAttributes.Add(attribute);
return newSd;
}