本文整理汇总了C#中CustomAttribute类的典型用法代码示例。如果您正苦于以下问题:C# CustomAttribute类的具体用法?C# CustomAttribute怎么用?C# CustomAttribute使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
CustomAttribute类属于命名空间,在下文中一共展示了CustomAttribute类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CompareConstructorArguments
private static int CompareConstructorArguments(CustomAttribute first, CustomAttribute second)
{
if (first.HasConstructorArguments && !second.HasConstructorArguments)
{
return 1;
}
if (!first.HasConstructorArguments && second.HasConstructorArguments)
{
return -1;
}
int maxArguments = Math.Max(first.ConstructorArguments.Count, second.ConstructorArguments.Count);
for (int i = 0; i < maxArguments; i++)
{
if (first.ConstructorArguments.Count <= i)
{
return 1;
}
if (second.ConstructorArguments.Count <= i)
{
return -1;
}
CustomAttributeArgument firstArgument = first.ConstructorArguments[i];
CustomAttributeArgument secondArgument = second.ConstructorArguments[i];
int compareResult = CompareCustomAttributeArguments(firstArgument, secondArgument);
if (compareResult != 0)
{
return compareResult;
}
}
return 0;
}
示例2: IsIndexer
public static bool IsIndexer(this PropertyDefinition property, out CustomAttribute defaultMemberAttribute)
{
defaultMemberAttribute = null;
if (!property.HasParameters)
return false;
var accessor = property.GetMethod ?? property.SetMethod;
var basePropDef = property;
if (accessor.HasOverrides)
{
// if the property is explicitly implementing an interface, look up the property in the interface:
var baseAccessor = accessor.Overrides.First().Resolve();
if (baseAccessor != null)
{
foreach (var baseProp in baseAccessor.DeclaringType.Properties.Where(baseProp => baseProp.GetMethod == baseAccessor || baseProp.SetMethod == baseAccessor))
{
basePropDef = baseProp;
break;
}
}
else
return false;
}
CustomAttribute attr;
var defaultMemberName = basePropDef.DeclaringType.GetDefaultMemberName(out attr);
if (defaultMemberName != basePropDef.Name)
return false;
defaultMemberAttribute = attr;
return true;
}
示例3: Construct
/// <summary>
/// Generates a proxy that forwards all virtual method calls
/// to a single <see cref="IInterceptor"/> instance.
/// </summary>
/// <param name="originalBaseType">The base class of the type being constructed.</param>
/// <param name="baseInterfaces">The list of interfaces that the new type must implement.</param>
/// <param name="module">The module that will hold the brand new type.</param>
/// <param name="targetType">The <see cref="TypeDefinition"/> that represents the type to be created.</param>
public override void Construct(Type originalBaseType, IEnumerable<Type> baseInterfaces, ModuleDefinition module,
TypeDefinition targetType)
{
var interfaces = new HashSet<Type>(baseInterfaces);
if (!interfaces.Contains(typeof (ISerializable)))
interfaces.Add(typeof (ISerializable));
var serializableInterfaceType = module.ImportType<ISerializable>();
if (!targetType.Interfaces.Contains(serializableInterfaceType))
targetType.Interfaces.Add(serializableInterfaceType);
// Create the proxy type
base.Construct(originalBaseType, interfaces, module, targetType);
// Add the Serializable attribute
targetType.IsSerializable = true;
var serializableCtor = module.ImportConstructor<SerializableAttribute>();
var serializableAttribute = new CustomAttribute(serializableCtor);
targetType.CustomAttributes.Add(serializableAttribute);
ImplementGetObjectData(originalBaseType, baseInterfaces, module, targetType);
DefineSerializationConstructor(module, targetType);
var interceptorType = module.ImportType<IInterceptor>();
var interceptorGetterProperty = (from PropertyDefinition m in targetType.Properties
where
m.Name == "Interceptor" &&
m.PropertyType == interceptorType
select m).First();
}
示例4: Write
/// <summary>
/// Writes a custom attribute
/// </summary>
/// <param name="helper">Helper class</param>
/// <param name="ca">The custom attribute</param>
/// <returns>Custom attribute blob</returns>
public static byte[] Write(ICustomAttributeWriterHelper helper, CustomAttribute ca)
{
using (var writer = new CustomAttributeWriter(helper)) {
writer.Write(ca);
return writer.GetResult();
}
}
示例5: createSSAEktronMember
public void createSSAEktronMember(string PIN)
{
//try
//{
Dictionary<string, string> UserDetails = loginSSA.GetUsersDetails(PIN);
UserManager Usermanager = new UserManager();
CustomAttributeList attrList = new CustomAttributeList();
CustomAttribute timeZone = new CustomAttribute();
timeZone.Name = "Time Zone";
timeZone.Value = "Eastern Standard Time";
attrList.Add(timeZone);
UserData newUserdata = new UserData()
{
Username = PIN,
Password = EktronMemberDefaultPassword,
FirstName = UserDetails["FirstName"],
LastName = UserDetails["LastName"],
DisplayName = UserDetails["DisplayName"],
Email = UserDetails["Email"],
CustomProperties = attrList,
// IsMemberShip = true
};
if (Ektron.Cms.Framework.Context.UserContextService.Current.IsLoggedIn)
{
Usermanager.Add(newUserdata);
// add user to group MSBA Members
UserGroupManager UserGroupmanager = new UserGroupManager();
//Add a User to a UserGroup
UserGroupmanager.AddUser(1, newUserdata.Id);
}
}
示例6: CompareToCustomAttribute
public static int CompareToCustomAttribute(this CustomAttribute first, CustomAttribute second, bool fullNameCheck = false)
{
if (first.AttributeType.Name != second.AttributeType.Name)
{
if (fullNameCheck)
{
return first.AttributeType.FullName.CompareTo(second.AttributeType.FullName);
}
return first.AttributeType.Name.CompareTo(second.AttributeType.Name);
}
if (first == second)
{
return 0;
}
int returnValue = CompareConstructorArguments(first, second);
if(returnValue != 0)
{
return returnValue;
}
returnValue = CompareConstructorFields(first, second);
if (returnValue != 0)
{
return returnValue;
}
returnValue = CompareConstructorProperties(first, second);
if (returnValue != 0)
{
return returnValue;
}
return 0;
}
示例7: FromReader
public static CustomAttributeSignature FromReader(CustomAttribute parent, IBinaryStreamReader reader)
{
long position = reader.Position;
if (!reader.CanRead(sizeof (ushort)) || reader.ReadUInt16() != 0x0001)
throw new ArgumentException("Signature doesn't refer to a valid custom attribute signature.");
var signature = new CustomAttributeSignature()
{
StartOffset = position,
};
if (parent.Constructor != null)
{
var methodSignature = parent.Constructor.Signature as MethodSignature;
if (methodSignature != null)
{
foreach (var parameter in methodSignature.Parameters)
{
signature.FixedArguments.Add(CustomAttributeArgument.FromReader(parent.Header,
parameter.ParameterType, reader));
}
}
}
var namedElementCount = reader.CanRead(sizeof (ushort)) ? reader.ReadUInt16() : 0;
for (uint i = 0; i < namedElementCount; i++)
{
signature.NamedArguments.Add(CustomAttributeNamedArgument.FromReader(parent.Header, reader));
}
return signature;
}
示例8: CilCustomAttribute
internal CilCustomAttribute(CustomAttribute attribute, ref CilReaders readers)
{
_attribute = attribute;
_readers = readers;
_parent = null;
_constructor = null;
_value = null;
}
示例9: Decorate
public void Decorate(MethodDefinition method, CustomAttribute attribute)
{
method.Body.InitLocals = true;
var getMethodFromHandleRef = referenceFinder.GetMethodReference(typeof(MethodBase), md => md.Name == "GetMethodFromHandle" && md.Parameters.Count == 2);
var getCustomAttributesRef = referenceFinder.GetMethodReference(typeof(MemberInfo), md => md.Name == "GetCustomAttributes" && md.Parameters.Count == 2);
var getTypeFromHandleRef = referenceFinder.GetMethodReference(typeof(Type), md => md.Name == "GetTypeFromHandle");
var methodBaseTypeRef = referenceFinder.GetTypeReference(typeof(MethodBase));
var exceptionTypeRef = referenceFinder.GetTypeReference(typeof(Exception));
var methodVariableDefinition = AddVariableDefinition(method, "__fody$method", methodBaseTypeRef);
var attributeVariableDefinition = AddVariableDefinition(method, "__fody$attribute", attribute.AttributeType);
var exceptionVariableDefinition = AddVariableDefinition(method, "__fody$exception", exceptionTypeRef);
VariableDefinition retvalVariableDefinition = null;
if (method.ReturnType.FullName != "System.Void")
retvalVariableDefinition = AddVariableDefinition(method, "__fody$retval", method.ReturnType);
var onEntryMethodRef = referenceFinder.GetMethodReference(attribute.AttributeType, md => md.Name == "OnEntry");
var onExitMethodRef = referenceFinder.GetMethodReference(attribute.AttributeType, md => md.Name == "OnExit");
var onExceptionMethodRef = referenceFinder.GetMethodReference(attribute.AttributeType, md => md.Name == "OnException");
var processor = method.Body.GetILProcessor();
var methodBodyFirstInstruction = method.Body.Instructions.First();
if (method.IsConstructor)
methodBodyFirstInstruction = method.Body.Instructions.First(i => i.OpCode == OpCodes.Call).Next;
var getAttributeInstanceInstructions = GetAttributeInstanceInstructions(processor, method, attribute, attributeVariableDefinition, methodVariableDefinition, getCustomAttributesRef, getTypeFromHandleRef, getMethodFromHandleRef);
var callOnEntryInstructions = GetCallOnEntryInstructions(processor, attributeVariableDefinition, methodVariableDefinition, onEntryMethodRef);
var saveRetvalInstructions = GetSaveRetvalInstructions(processor, retvalVariableDefinition);
var callOnExitInstructions = GetCallOnExitInstructions(processor, attributeVariableDefinition, methodVariableDefinition, onExitMethodRef);
var methodBodyReturnInstructions = GetMethodBodyReturnInstructions(processor, retvalVariableDefinition);
var methodBodyReturnInstruction = methodBodyReturnInstructions.First();
var tryCatchLeaveInstructions = GetTryCatchLeaveInstructions(processor, methodBodyReturnInstruction);
var catchHandlerInstructions = GetCatchHandlerInstructions(processor, attributeVariableDefinition, exceptionVariableDefinition, methodVariableDefinition, onExceptionMethodRef);
ReplaceRetInstructions(processor, saveRetvalInstructions.Concat(callOnExitInstructions).First());
processor.InsertBefore(methodBodyFirstInstruction, getAttributeInstanceInstructions);
processor.InsertBefore(methodBodyFirstInstruction, callOnEntryInstructions);
processor.InsertAfter(method.Body.Instructions.Last(), methodBodyReturnInstructions);
processor.InsertBefore(methodBodyReturnInstruction, saveRetvalInstructions);
processor.InsertBefore(methodBodyReturnInstruction, callOnExitInstructions);
processor.InsertBefore(methodBodyReturnInstruction, tryCatchLeaveInstructions);
processor.InsertBefore(methodBodyReturnInstruction, catchHandlerInstructions);
method.Body.ExceptionHandlers.Add(new ExceptionHandler(ExceptionHandlerType.Catch)
{
CatchType = exceptionTypeRef,
TryStart = methodBodyFirstInstruction,
TryEnd = tryCatchLeaveInstructions.Last().Next,
HandlerStart = catchHandlerInstructions.First(),
HandlerEnd = catchHandlerInstructions.Last().Next
});
}
示例10: Write
void Write(CustomAttribute ca) {
if (ca == null) {
helper.Error("The custom attribute is null");
return;
}
// Check whether it's raw first. If it is, we don't care whether the ctor is
// invalid. Just use the raw data.
if (ca.IsRawBlob) {
if ((ca.ConstructorArguments != null && ca.ConstructorArguments.Count > 0) || (ca.NamedArguments != null && ca.NamedArguments.Count > 0))
helper.Error("Raw custom attribute contains arguments and/or named arguments");
writer.Write(ca.RawData);
return;
}
if (ca.Constructor == null) {
helper.Error("Custom attribute ctor is null");
return;
}
var methodSig = GetMethodSig(ca.Constructor);
if (methodSig == null) {
helper.Error("Custom attribute ctor's method signature is invalid");
return;
}
if (ca.ConstructorArguments.Count != methodSig.Params.Count)
helper.Error("Custom attribute arguments count != method sig arguments count");
if (methodSig.ParamsAfterSentinel != null && methodSig.ParamsAfterSentinel.Count > 0)
helper.Error("Custom attribute ctor has parameters after the sentinel");
if (ca.NamedArguments.Count > ushort.MaxValue)
helper.Error("Custom attribute has too many named arguments");
// A generic custom attribute isn't allowed by most .NET languages (eg. C#) but
// the CLR probably supports it.
var mrCtor = ca.Constructor as MemberRef;
if (mrCtor != null) {
var owner = mrCtor.Class as TypeSpec;
if (owner != null) {
var gis = owner.TypeSig as GenericInstSig;
if (gis != null) {
genericArguments = new GenericArguments();
genericArguments.PushTypeArgs(gis.GenericArguments);
}
}
}
writer.Write((ushort)1);
int numArgs = Math.Min(methodSig.Params.Count, ca.ConstructorArguments.Count);
for (int i = 0; i < numArgs; i++)
WriteValue(FixTypeSig(methodSig.Params[i]), ca.ConstructorArguments[i]);
int numNamedArgs = Math.Min((int)ushort.MaxValue, ca.NamedArguments.Count);
writer.Write((ushort)numNamedArgs);
for (int i = 0; i < numNamedArgs; i++)
Write(ca.NamedArguments[i]);
}
示例11: ToHostAttributeMapping
private HostAttributeMapping ToHostAttributeMapping(CustomAttribute arg) {
var prms = arg.ConstructorArguments.First().Value as CustomAttributeArgument[];
if (null == prms)
return null;
return new HostAttributeMapping {
HostAttribute = arg,
AttribyteTypes = prms.Select(c => ((TypeReference)c.Value).Resolve()).ToArray()
};
}
示例12: GetSetIsChanged
bool? GetSetIsChanged(CustomAttribute notifyAttribute)
{
var setIsChanged = notifyAttribute.Properties.FirstOrDefault(x => x.Name == "SetIsChanged");
var setIsChangedValue = setIsChanged.Argument.Value;
if (setIsChangedValue != null)
{
return (bool) setIsChangedValue;
}
return null;
}
示例13: AddEditorBrowsableAttribute
void AddEditorBrowsableAttribute(Collection<CustomAttribute> customAttributes)
{
if (customAttributes.Any(x=>x.AttributeType.Name == "EditorBrowsableAttribute"))
{
return;
}
var customAttribute = new CustomAttribute(EditorBrowsableConstructor);
customAttribute.ConstructorArguments.Add(new CustomAttributeArgument(EditorBrowsableStateType, AdvancedStateConstant));
customAttributes.Add(customAttribute);
}
示例14: GetCheckForEquality
bool? GetCheckForEquality(CustomAttribute notifyAttribute)
{
var performEqualityCheck = notifyAttribute.Properties.FirstOrDefault(x => x.Name == "PerformEqualityCheck");
var equalityCheckValue = performEqualityCheck.Argument.Value;
if (equalityCheckValue != null)
{
return (bool) equalityCheckValue;
}
return null;
}
示例15: ImportExternal
void ImportExternal(CustomAttribute validationTemplateAttribute)
{
var typeReference = (TypeReference) validationTemplateAttribute.ConstructorArguments.First().Value;
TypeDefinition = typeReference.Resolve();
TypeReference = ModuleDefinition.ImportReference(TypeDefinition);
TemplateConstructor = ModuleDefinition.ImportReference(FindConstructor(TypeDefinition));
ModuleDefinition
.Assembly
.CustomAttributes.Remove(validationTemplateAttribute);
}