本文整理汇总了C#中MethodAttributes类的典型用法代码示例。如果您正苦于以下问题:C# MethodAttributes类的具体用法?C# MethodAttributes怎么用?C# MethodAttributes使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
MethodAttributes类属于命名空间,在下文中一共展示了MethodAttributes类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DefineMethod
public static MethodBuilder DefineMethod(this TypeBuilder typeBuilder, MethodInfo method, MethodAttributes? attributes = null, ParameterInfo[] parameters = null)
{
Type[] parametersTypes = null;
MethodBuilder methodBuilder = null;
parameters = parameters ?? method.GetParameters();
parametersTypes = parameters.ToArray(parameter => parameter.ParameterType);
attributes = attributes ?? method.Attributes & ~MethodAttributes.Abstract;
methodBuilder = typeBuilder.DefineMethod(method.Name, attributes.Value, method.ReturnType, parametersTypes);
parameters.ForEach(1, (parameter, i) => {
var parameterBuilder = methodBuilder.DefineParameter(i, parameter.Attributes, parameter.Name);
if (parameter.IsDefined<ParamArrayAttribute>()) {
parameterBuilder.SetCustomAttribute<ParamArrayAttribute>();
}
else if (parameter.IsOut) {
parameterBuilder.SetCustomAttribute<OutAttribute>();
}
else if (parameter.IsOptional) {
parameterBuilder.SetCustomAttribute<OptionalAttribute>()
.SetConstant(parameter.DefaultValue);
}
});
return methodBuilder;
}
示例2: DefineMethod
public void DefineMethod(string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes)
{
bool defaultReturnTypeAndParameters = returnType == null && parameterTypes == null;
if (callingConvention == CallingConventions.Standard)
{
if (defaultReturnTypeAndParameters)
{
// Use DefineMethod(string, MethodAttributes)
TypeBuilder type1 = Helpers.DynamicType(TypeAttributes.Public);
MethodBuilder method1 = type1.DefineMethod(name, attributes);
VerifyMethod(type1, method1, name, attributes, callingConvention, returnType, parameterTypes);
}
// Use DefineMethod(string, MethodAttributes, Type, Type[])
TypeBuilder type2 = Helpers.DynamicType(TypeAttributes.Public);
MethodBuilder method2 = type2.DefineMethod(name, attributes, returnType, parameterTypes);
VerifyMethod(type2, method2, name, attributes, callingConvention, returnType, parameterTypes);
}
if (defaultReturnTypeAndParameters)
{
// Use DefineMethod(string, MethodAttributes, CallingConventions)
TypeBuilder type3 = Helpers.DynamicType(TypeAttributes.Public);
MethodBuilder method3 = type3.DefineMethod(name, attributes, callingConvention);
VerifyMethod(type3, method3, name, attributes, callingConvention, returnType, parameterTypes);
}
// Use DefineMethod(string, MethodAttributes, CallingConventions, Type, Type[])
TypeBuilder type4 = Helpers.DynamicType(TypeAttributes.Public);
MethodBuilder method4 = type4.DefineMethod(name, attributes, callingConvention, returnType, parameterTypes);
VerifyMethod(type4, method4, name, attributes, callingConvention, returnType, parameterTypes);
}
示例3: PropertyKey
public PropertyKey( TypeKey typeKey, PropertyDefinition prop )
{
this.typeKey = typeKey;
this.type = prop.PropertyType.FullName;
this.name = prop.Name;
this.getterMethodAttributes = prop.GetMethod != null ? prop.GetMethod.Attributes : 0;
}
示例4: ExecutePosTest
private object[] ExecutePosTest(
CustomAttributeBuilder customAttrBuilder,
MethodAttributes getMethodAttr,
Type returnType,
Type[] paramTypes,
BindingFlags bindingAttr)
{
TypeBuilder myTypeBuilder = GetTypeBuilder(TypeAttributes.Class | TypeAttributes.Public);
PropertyBuilder myPropertyBuilder = myTypeBuilder.DefineProperty(DynamicPropertyName,
PropertyAttributes.HasDefault,
returnType, null);
myPropertyBuilder.SetCustomAttribute(customAttrBuilder);
// Define the "get" accessor method for DynamicPropertyName
MethodBuilder myMethodBuilder = myTypeBuilder.DefineMethod(DynamicMethodName,
getMethodAttr, returnType, paramTypes);
ILGenerator methodILGenerator = myMethodBuilder.GetILGenerator();
methodILGenerator.Emit(OpCodes.Ldarg_0);
methodILGenerator.Emit(OpCodes.Ret);
// Map the 'get' method created above to our PropertyBuilder
myPropertyBuilder.SetGetMethod(myMethodBuilder);
Type myType = myTypeBuilder.CreateTypeInfo().AsType();
PropertyInfo myProperty = myType.GetProperty(DynamicPropertyName, bindingAttr);
return myProperty.GetCustomAttributes(false).Select(a => (object)a).ToArray();
}
示例5: DefineConstructor
/// <summary>
/// Defines a constructor.
/// </summary>
/// <param name="typeBuilder">The type builder.</param>
/// <param name="methodAttributes">The method attributes.</param>
/// <param name="callingConvention">The calling convention.</param>
/// <param name="parameterTypes">The parameter types.</param>
/// <param name="parameterNames">The parameter names.</param>
/// <returns>The constructor builder.</returns>
public static ConstructorBuilder DefineConstructor(this TypeBuilder typeBuilder,
MethodAttributes methodAttributes,
CallingConventions callingConvention,
Type[] parameterTypes,
string[] parameterNames)
{
if (typeBuilder == null)
throw new ArgumentNullException("typeBuilder");
if (parameterTypes == null)
throw new ArgumentNullException("parameterTypes");
if (parameterNames == null)
throw new ArgumentNullException("parameterNames");
if (parameterTypes.Length != parameterNames.Length)
throw new ArgumentException(Resources.NumberOfParameterTypesAndNamesMustBeEqual);
// Define constructor.
var constructorBuilder = typeBuilder.DefineConstructor(
methodAttributes,
callingConvention,
parameterTypes);
// Define constructor parameters.
constructorBuilder.DefineParameters(parameterNames);
return constructorBuilder;
}
示例6: PosTest1
public void PosTest1()
{
int i = 0;
MethodAttributes[] attributes = new MethodAttributes[] {
MethodAttributes.Assembly,
MethodAttributes.CheckAccessOnOverride,
MethodAttributes.FamANDAssem,
MethodAttributes.Family,
MethodAttributes.FamORAssem,
MethodAttributes.Final,
MethodAttributes.HasSecurity,
MethodAttributes.HideBySig,
MethodAttributes.MemberAccessMask,
MethodAttributes.NewSlot,
MethodAttributes.Private,
MethodAttributes.PrivateScope,
MethodAttributes.Public,
MethodAttributes.RequireSecObject,
MethodAttributes.ReuseSlot,
MethodAttributes.RTSpecialName,
MethodAttributes.SpecialName,
MethodAttributes.Static,
MethodAttributes.UnmanagedExport,
MethodAttributes.Virtual,
MethodAttributes.VtableLayoutMask
};
for (; i < attributes.Length; ++i)
{
ILGenerator generator =
CreateConstructorBuilder("PosTest1_Type" + i, attributes[i]).GetILGenerator();
Assert.NotNull(generator);
}
}
示例7: CreateFunction
public ILGenerator CreateFunction(TypeBuilder a_TypeBuilder, string a_Name, MethodAttributes a_MethodAttributes, Type a_ReturnType, Type[] a_Parameters)
{
MethodBuilder methodBuilder = a_TypeBuilder.DefineMethod(a_Name, a_MethodAttributes, a_ReturnType, a_Parameters);
ILGenerator ilGenerator = methodBuilder.GetILGenerator();
return ilGenerator;
}
示例8: CreateConstructor
/// <summary>
/// 타입의 생성자를 생성합니다.
/// </summary>
/// <param name="methodAttributes"> 생성자 메서드인 .ctor 의 메서드 특성입니다. </param>
/// <param name="callingConventions"> 메서드의 유효한 호출 규칙입니다. </param>
/// <param name="parameterCriteriaMetadataInfos"> 매개 변수의 표준적인 메타데이터 정보입니다. </param>
/// <returns>
/// 생성자를 생성할 때 사용하는 <see cref="ConstructorBuilder"/> 객체를 반환합니다.
/// </returns>
public ConstructorBuilder CreateConstructor(MethodAttributes methodAttributes, CallingConventions callingConventions, IEnumerable<ParameterCriteriaMetadataInfo> parameterCriteriaMetadataInfos)
{
if (isStaticMethod(methodAttributes))
{
methodAttributes = MethodAttributes.Static | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName | MethodAttributes.Private | MethodAttributes.HideBySig;
callingConventions = CallingConventions.Standard;
}
else
{
callingConventions = CallingConventions.HasThis;
}
var constructorBuilder = this.TypeBuilder.DefineConstructor(methodAttributes, callingConventions, parameterCriteriaMetadataInfos.Select( o => o.Type).ToArray());
int iSeqence = 0;
foreach (var parameter in parameterCriteriaMetadataInfos)
{
iSeqence++;
constructorBuilder.DefineParameter(iSeqence, parameter.ParameterAttribute, parameter.Name);
}
var il = constructorBuilder.GetILGenerator();
if (isStaticMethod(methodAttributes)) // 정적 생성자는 Object 개체 파생이 아니므로 Object 생성을 하지 않음
{
il.Emit(OpCodes.Nop);
}
else
{
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Call, this.TypeBuilder.BaseType.GetConstructors()[0]);
}
return constructorBuilder;
}
示例9: EventKey
public EventKey( TypeKey typeKey, string type, string name, MethodAttributes addMethodAttributes)
{
this.typeKey = typeKey;
this.type = type;
this.name = name;
this.addMethodAttributes = addMethodAttributes;
}
示例10: NonTargetedMethodMetadata
public NonTargetedMethodMetadata(MethodInfo method)
: base(method)
{
_methodAttributes = method.Attributes;
_methodAttributes &= ~MethodAttributes.Abstract;
_methodAttributes &= ~MethodAttributes.NewSlot;
}
示例11: AddPropertyGetter
public static MethodDefinition AddPropertyGetter(
PropertyDefinition property
, MethodAttributes methodAttributes = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName | MethodAttributes.NewSlot | MethodAttributes.Virtual
, FieldDefinition backingField = null)
{
if (backingField == null)
{
// TODO: Try and find existing friendly named backingFields first.
backingField = AddPropertyBackingField(property);
}
var methodName = "get_" + property.Name;
var getter = new MethodDefinition(methodName, methodAttributes, property.PropertyType)
{
IsGetter = true,
Body = {InitLocals = true},
};
getter.Body.Variables.Add(new VariableDefinition(property.PropertyType));
var returnStart = Instruction.Create(OpCodes.Ldloc_0);
getter.Body.Instructions.Append(
Instruction.Create(OpCodes.Ldarg_0),
Instruction.Create(OpCodes.Ldfld, backingField),
Instruction.Create(OpCodes.Stloc_0),
Instruction.Create(OpCodes.Br_S, returnStart),
returnStart,
Instruction.Create(OpCodes.Ret)
);
property.GetMethod = getter;
property.DeclaringType.Methods.Add(getter);
return getter;
}
示例12: ConstructorBuilder
/// <summary>
/// Constructor
/// </summary>
/// <param name="typeBuilder">Type builder</param>
/// <param name="attributes">Attributes for the constructor (public, private, etc.)</param>
/// <param name="parameters">Parameter types for the constructor</param>
/// <param name="callingConventions">Calling convention for the constructor</param>
public ConstructorBuilder(TypeBuilder typeBuilder, MethodAttributes attributes,
IEnumerable<Type> parameters, CallingConventions callingConventions)
{
if (typeBuilder == null)
throw new ArgumentNullException("typeBuilder");
Type = typeBuilder;
Attributes = attributes;
Parameters = new List<ParameterBuilder>();
Parameters.Add(new ParameterBuilder(null, 0));
if (parameters != null)
{
int x = 1;
foreach (var parameterType in parameters)
{
Parameters.Add(new ParameterBuilder(parameterType, x));
++x;
}
}
CallingConventions = callingConventions;
Builder = Type.Builder.DefineConstructor(attributes, callingConventions,
(parameters != null && parameters.Count() > 0)
? parameters.ToArray()
: System.Type.EmptyTypes);
Generator = Builder.GetILGenerator();
}
示例13: CheckSupport
private void CheckSupport(EventDefinition eventInfo, MethodAttributes methodAttributes)
{
EventAttributes eventAttributes = eventInfo.Attributes;
string warningTemplate = "Event '" + name + "' has unsupported attribute: '{0}'.";
// in order to reduce output we warn only about important attributes which are not currently
// supported:
// EventDefinition properties
// EventAttributes
//if ((eventAttributes & EventAttributes.RTSpecialName) != 0) { Logger.Warning(warningTemplate, "RTSpecialName"); }
//if ((eventAttributes & EventAttributes.SpecialName) != 0) { Logger.Warning(warningTemplate, "SpecialName"); }
// MethodAttributes
//if ((methodAttributes & MethodAttributes.CheckAccessOnOverride) != 0) { Logger.Warning(warningTemplate, "CheckAccessOnOverride"); }
//if ((methodAttributes & MethodAttributes.FamANDAssem) != 0) { Logger.Warning(warningTemplate, "FamANDAssem"); }
// TODO: support this: if ((methodAttributes & MethodAttributes.HasSecurity) != 0) { Logger.Warning(warningTemplate, "HasSecurity"); }
//if ((methodAttributes & MethodAttributes.HideBySig) != 0) { Logger.Warning(warningTemplate, "HideBySig"); }
//if ((methodAttributes & MethodAttributes.NewSlot) != 0) { Logger.Warning(warningTemplate, "NewSlot"); }
// TODO: support this: if ((methodAttributes & MethodAttributes.PinvokeImpl) != 0) { Logger.Warning(warningTemplate, "PinvokeImpl"); }
//if ((methodAttributes & MethodAttributes.PrivateScope) != 0) { Logger.Warning(warningTemplate, "PrivateScope"); }
// TODO: support this: if ((methodAttributes & MethodAttributes.RequireSecObject) != 0) { Logger.Warning(warningTemplate, "RequiresSecObject"); }
//if ((methodAttributes & MethodAttributes.ReuseSlot) != 0) { Logger.Warning(warningTemplate, "ReuseSlot"); }
//if ((methodAttributes & MethodAttributes.RTSpecialName) != 0) { Logger.Warning(warningTemplate, "RTSpecialName"); }
//if ((methodAttributes & MethodAttributes.SpecialName) != 0) { Logger.Warning(warningTemplate, "SpecialName"); }
// TODO: support this: if ((methodAttributes & MethodAttributes.UnmanagedExport) != 0) { Logger.Warning(warningTemplate, "UnmanagedExport"); }
}
示例14: FunctionObject
internal FunctionObject(Type t, string name, string method_name, string[] formal_parameters, JSLocalField[] fields, bool must_save_stack_locals, bool hasArgumentsObject, string text, VsaEngine engine) : base(engine.Globals.globalObject.originalFunction.originalPrototype, name, formal_parameters.Length)
{
base.engine = engine;
this.formal_parameters = formal_parameters;
this.argumentsSlotNumber = 0;
this.body = null;
this.method = TypeReflector.GetTypeReflectorFor(Globals.TypeRefs.ToReferenceContext(t)).GetMethod(method_name, BindingFlags.Public | BindingFlags.Static);
this.parameterInfos = this.method.GetParameters();
if (!Microsoft.JScript.CustomAttribute.IsDefined(this.method, typeof(JSFunctionAttribute), false))
{
this.isMethod = true;
}
else
{
JSFunctionAttributeEnum attributeValue = ((JSFunctionAttribute) Microsoft.JScript.CustomAttribute.GetCustomAttributes(this.method, typeof(JSFunctionAttribute), false)[0]).attributeValue;
this.isExpandoMethod = (attributeValue & JSFunctionAttributeEnum.IsExpandoMethod) != JSFunctionAttributeEnum.None;
}
this.funcContext = null;
this.own_scope = null;
this.fields = fields;
this.must_save_stack_locals = must_save_stack_locals;
this.hasArgumentsObject = hasArgumentsObject;
this.text = text;
this.attributes = MethodAttributes.Public;
this.globals = engine.Globals;
this.superConstructor = null;
this.superConstructorCall = null;
this.enclosing_scope = this.globals.ScopeStack.Peek();
base.noExpando = false;
this.clsCompliance = CLSComplianceSpec.NotAttributed;
}
示例15: DefineConstructor
public void DefineConstructor(MethodAttributes methodAttributes, Type[] parameterTypes, CallingConventions callingConvention, BindingFlags bindingFlags)
{
TypeBuilder type = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public);
FieldBuilder fieldBuilderA = type.DefineField("TestField", typeof(int), FieldAttributes.Private);
FieldBuilder fieldBuilderB = type.DefineField("TestField", typeof(int), FieldAttributes.Private);
ConstructorBuilder ctorBuilder = type.DefineConstructor(methodAttributes, callingConvention, parameterTypes);
ILGenerator ctorIlGenerator = ctorBuilder.GetILGenerator();
if (parameterTypes.Length != 0)
{
//Calling base class constructor
ctorIlGenerator.Emit(OpCodes.Ldarg_0);
ctorIlGenerator.Emit(OpCodes.Call, typeof(object).GetConstructor(new Type[0]));
ctorIlGenerator.Emit(OpCodes.Ldarg_0);
ctorIlGenerator.Emit(OpCodes.Ldarg_1);
ctorIlGenerator.Emit(OpCodes.Stfld, fieldBuilderA);
ctorIlGenerator.Emit(OpCodes.Ldarg_0);
ctorIlGenerator.Emit(OpCodes.Ldarg_2);
ctorIlGenerator.Emit(OpCodes.Stfld, fieldBuilderB);
}
ctorIlGenerator.Emit(OpCodes.Ret);
Type createdType = type.CreateTypeInfo().AsType();
Assert.NotNull(createdType.GetConstructors(bindingFlags).FirstOrDefault());
}