// dnlib: See LICENSE.txt for more info
using System.Collections.Generic;
namespace dnlib.DotNet {
///
/// A DeclSecurity security attribute
///
public sealed class SecurityAttribute : ICustomAttribute {
ITypeDefOrRef attrType;
readonly IList namedArguments;
///
/// Gets/sets the attribute type
///
public ITypeDefOrRef AttributeType {
get => attrType;
set => attrType = value;
}
///
/// Gets the full name of the attribute type
///
public string TypeFullName {
get {
var at = attrType;
return at is null ? string.Empty : at.FullName;
}
}
///
/// Gets all named arguments (field and property values)
///
public IList NamedArguments => namedArguments;
///
/// true if is not empty
///
public bool HasNamedArguments => namedArguments.Count > 0;
///
/// Gets all s that are field arguments
///
public IEnumerable Fields {
get {
var namedArguments = this.namedArguments;
int count = namedArguments.Count;
for (int i = 0; i < count; i++) {
var namedArg = namedArguments[i];
if (namedArg.IsField)
yield return namedArg;
}
}
}
///
/// Gets all s that are property arguments
///
public IEnumerable Properties {
get {
var namedArguments = this.namedArguments;
int count = namedArguments.Count;
for (int i = 0; i < count; i++) {
var namedArg = namedArguments[i];
if (namedArg.IsProperty)
yield return namedArg;
}
}
}
///
/// Creates a from an XML string.
///
/// Owner module
/// XML
/// A new instance
public static SecurityAttribute CreateFromXml(ModuleDef module, string xml) {
var attrType = module.CorLibTypes.GetTypeRef("System.Security.Permissions", "PermissionSetAttribute");
var utf8Xml = new UTF8String(xml);
var namedArg = new CANamedArgument(false, module.CorLibTypes.String, "XML", new CAArgument(module.CorLibTypes.String, utf8Xml));
var list = new List { namedArg };
return new SecurityAttribute(attrType, list);
}
///
/// Default constructor
///
public SecurityAttribute()
: this(null, null) {
}
///
/// Constructor
///
/// Attribute type
public SecurityAttribute(ITypeDefOrRef attrType)
: this(attrType, null) {
}
///
/// Constructor
///
/// Attribute type
/// Named arguments that will be owned by this instance
public SecurityAttribute(ITypeDefOrRef attrType, IList namedArguments) {
this.attrType = attrType;
this.namedArguments = namedArguments ?? new List();
}
///
public override string ToString() => TypeFullName;
}
}