Permissions must support XML encoding with the ISecurityEncodable interface, allowing the permission to be represented as XML and the permission object state restored from such an XML representation. For a complete description of encoding permissions, see the section on Encoding.
The content of the XML element that represents the object state is determined by the object itself – the FromXML method can use any representation so long as the ToXML can interpret it and restore the same state. However, the containing “Permission” element must be of standard form, e.g. for XyzPermission:
<Permission class=”SamplePermission, Xyz.dll” version=”1”>
The Permission element contains two attributes, “class” containing type name disambiguated by the name of the assembly that contains it, and the “version” attribute specifying the version. The following code fragment creates such a permission object XML element.
SecurityElement element = new SecurityElement( "Permission" ); Type type = this.GetType(); String assemblyName = type.Module.Assembly.FullName; assemblyName = assemblyName.Replace( '\"', '\'' ); element.AddAttribute( "class", type.FullName + ", " + assemblyName ); element.AddAttribute( "version", "1" );
Note the Replace method applied to the assembly name. Attributes in SecurityElement can't handle having quotes in them, but some assembly name information is quoted. To handle this situation, double quotes (") in the assembly name are converted to single quote (').