本文整理汇总了C#中MethodAttributes.HasFlag方法的典型用法代码示例。如果您正苦于以下问题:C# MethodAttributes.HasFlag方法的具体用法?C# MethodAttributes.HasFlag怎么用?C# MethodAttributes.HasFlag使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MethodAttributes
的用法示例。
在下文中一共展示了MethodAttributes.HasFlag方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: StartMethod
internal void StartMethod(string name, Type returns, Type[] parameters, MethodAttributes methodAttributes)
{
var before = "";
var firstArgIdx = 1;
if (methodAttributes.HasFlag(MethodAttributes.Public)) before += "public ";
if (methodAttributes.HasFlag(MethodAttributes.Static))
{
firstArgIdx = 0;
before += "static ";
}
if (methodAttributes.HasFlag(MethodAttributes.Virtual)) before += "virtual ";
switch (parameters.Length)
{
case 0:
WriteLine(String.Format("{2}{0} {1}()", returns.ToSimpleName(), name, before));
break;
case 1:
WriteLine(String.Format("{2}{0} {1}({3} arg{4})", returns.ToSimpleName(), name, before, parameters[0].ToSimpleName(), firstArgIdx));
break;
default:
WriteLine(String.Format("{2}{0} {1}(", returns.ToSimpleName(), name, before));
Indent++;
int idx = 0;
foreach (var par in parameters)
{
WriteLine(
$"{par.ToSimpleName()} arg{idx + firstArgIdx}{(idx + 1 == parameters.Length ? ')' : ',')}");
idx++;
}
Indent--;
break;
}
OpenScope();
}
示例2: GetRMethodAttributes
public static RMethodAttributes GetRMethodAttributes(MethodAttributes attributes)
{
RMethodAttributes attrs = 0;
if (attributes.HasFlag(MethodAttributes.Private))
{
attrs |= RMethodAttributes.Private;
}
else
{
attrs |= RMethodAttributes.Public;
}
if (attributes.HasFlag(MethodAttributes.NewSlot))
{
attrs |= RMethodAttributes.Override;
}
return attrs;
}
示例3: DefineProperty
/// <summary>
/// Defines a new property to the type.
/// </summary>
/// <param name="name">The name of the property. <paramref name="name"/> cannot contain embedded nulls.</param>
/// <param name="type">The type of the property.</param>
/// <param name="methodAttributes">A bitwise combination of the accessor method attributes.</param>
/// <param name="initializer">The expression which is not reduced to be <see cref="LambdaExpression"/> for the initializer of the backing field, with a parameter for "this" instance, returns <paramref name="type"/> value.</param>
/// <param name="getter">The expression which is not reduced to be <see cref="LambdaExpression"/> for the body of the getter of the property, with parameters for "this" instance, returns <paramref name="type"/> value, <see cref="IgnoredExpression"/> if the getter will be auto-implemented and accesses to the backing field, or <c>null</c> if this property does not have getter.</param>
/// <param name="setter">The expression which is not reduced to be <see cref="LambdaExpression"/> for the body of the setter of the property, with parameters for "this" instance and <paramref name="type"/> value, returns no value, <see cref="IgnoredExpression"/> if the setter will be auto-implemented and accesses to the backing field, or <c>null</c> if this property does not have setter.</param>
/// <returns>The defined property.</returns>
public PropertyBuilder DefineProperty(
String name,
Type type,
MethodAttributes methodAttributes,
Expression initializer,
Expression getter,
Expression setter
)
{
var isStatic = methodAttributes.HasFlag(MethodAttributes.Static);
return this._type.DefineProperty(
name,
PropertyAttributes.HasDefault,
type,
Type.EmptyTypes
)
.Apply(p =>
(getter is IgnoredExpression && setter is IgnoredExpression
? null
: this.DefineField(
GetName(p, "Field"),
type,
FieldAttributes.Private | (isStatic ? FieldAttributes.Static : 0),
initializer
)
.Apply(f => f.SetCustomAttribute(new CustomAttributeBuilder(
typeof(CompilerGeneratedAttribute).GetConstructor(Type.EmptyTypes),
Arrays.Empty<Object>()
)))
)
.Apply(
f => getter.Null(e => p.SetGetMethod(!(e as ListExpression).Null(l => l.Elements.IsEmpty())
? this.DefineMethod(
"get_" + name,
methodAttributes | MethodAttributes.HideBySig | MethodAttributes.SpecialName | (isStatic ? 0 : MethodAttributes.Virtual),
type,
Type.EmptyTypes,
e
)
: this._type.DefineMethod(
"get_" + name,
methodAttributes | MethodAttributes.HideBySig | MethodAttributes.SpecialName | (isStatic ? 0 : MethodAttributes.Virtual),
type,
Type.EmptyTypes
)
.Apply(
m => m.GetILGenerator().Apply(
g => g.If(_ => !isStatic, _ => LoadArgs(g, 0)),
g => g.Emit(isStatic ? OpCodes.Ldsfld : OpCodes.Ldfld, f),
g => g.Emit(OpCodes.Ret)
),
this._members.Add
)
)),
f => setter.Null(e => p.SetSetMethod(!(e as ListExpression).Null(l => l.Elements.IsEmpty())
? this.DefineMethod(
"set_" + name,
methodAttributes | MethodAttributes.HideBySig | MethodAttributes.SpecialName | (isStatic ? 0 : MethodAttributes.Virtual),
typeof(void),
new [] { type, },
e
)
: this._type.DefineMethod(
"set_" + name,
methodAttributes | MethodAttributes.HideBySig | MethodAttributes.SpecialName | (isStatic ? 0 : MethodAttributes.Virtual),
typeof(void),
new [] { type, }
)
.Apply(
m => m.GetILGenerator().Apply(
g => LoadArgs(g, 0),
g => g.If(_ => !isStatic, _ => LoadArgs(g, 1)),
g => g.Emit(isStatic ? OpCodes.Stsfld : OpCodes.Stfld, f),
g => g.Emit(OpCodes.Ret)
),
this._members.Add
)
))
),
this._members.Add
);
}
示例4: DefineMethod
/// <summary>
/// Defines a new method to the type.
/// </summary>
/// <param name="name">The name of the method. <paramref name="name"/> cannot contain embedded nulls.</param>
/// <param name="attributes">A bitwise combination of the method attributes.</param>
/// <param name="returnType">The return type of the method.</param>
/// <param name="parameterTypes">The types of the parameters of the method.</param>
/// <param name="body">The expression which is not reduced to be <see cref="LambdaExpression"/> for the body of the method, with parameters for "this" instance and all method parameters, returns <paramref name="returnType"/> value.</param>
/// <returns>The defined method.</returns>
public MethodBuilder DefineMethod(
String name,
MethodAttributes attributes,
Type returnType,
IList<Type> parameterTypes,
Expression body
)
{
var isStatic = attributes.HasFlag(MethodAttributes.Static);
if (returnType == null)
{
returnType = typeof(void);
}
if (parameterTypes == null)
{
parameterTypes = Type.EmptyTypes;
}
return this._type.DefineMethod(
name,
attributes | MethodAttributes.HideBySig | (isStatic ? 0 : MethodAttributes.Virtual),
returnType,
parameterTypes.ToArray()
)
.If(
m => body != null,
m => this._implType.DefineMethod(
GetName(m, "Impl"),
MethodAttributes.Assembly | MethodAttributes.Static,
returnType,
(isStatic
? parameterTypes
: parameterTypes.StartWith(_type)
).ToArray()
)
.Apply(
i => m.GetILGenerator().Apply(
g => LoadArgs(g, Enumerable.Range(0, parameterTypes.Count + (isStatic ? 0 : 1))),
g => g.Emit(OpCodes.Call, i),
g => g.Emit(OpCodes.Ret)
),
i => RequestInitializing(
i,
body,
returnType,
isStatic
? parameterTypes
: parameterTypes.StartWith(_type)
)
),
m => m.GetILGenerator().Apply(
g =>
{
if (returnType.IsValueType)
{
switch (Type.GetTypeCode(returnType))
{
case TypeCode.Byte:
case TypeCode.SByte:
case TypeCode.Char:
case TypeCode.UInt16:
case TypeCode.Int16:
case TypeCode.UInt32:
case TypeCode.Int32:
case TypeCode.Boolean:
g.Emit(OpCodes.Ldc_I4_0);
break;
case TypeCode.UInt64:
case TypeCode.Int64:
g.Emit(OpCodes.Ldc_I4_0);
g.Emit(OpCodes.Conv_I8);
break;
case TypeCode.Single:
g.Emit(OpCodes.Ldc_R4, (Single) 0);
break;
case TypeCode.Double:
g.Emit(OpCodes.Ldc_R8, (Double) 0);
break;
default:
if (returnType != typeof(void))
{
g.Emit(OpCodes.Ldloca_S, (Int16) 1);
g.Emit(OpCodes.Initobj, returnType);
}
break;
}
}
else
{
g.Emit(OpCodes.Ldnull);
}
g.Emit(OpCodes.Ret);
//.........这里部分代码省略.........
示例5: ModifyMethodProps
private void ModifyMethodProps(
MethodHandle methodDef,
ref string name,
ref MethodImplAttributes implFlags,
ref MethodAttributes flags,
ref int rva)
{
MethodDefTreatment treatment =
ComputeMethodDefTreatment(methodDef);
switch (treatment & MethodDefTreatment.TreatmentMask)
{
case MethodDefTreatment.Interface:
// Method is declared on an interface
implFlags |= MethodImplAttributes.Runtime |
MethodImplAttributes.InternalCall;
break;
case MethodDefTreatment.Delegate:
// Method is declared on a delegate
flags &= ~MethodAttributes.MemberAccessMask;
flags |= MethodAttributes.Public;
rva = 0;
implFlags |= MethodImplAttributes.Runtime;
break;
case MethodDefTreatment.Attribute:
// Method is declared on an attribute
rva = 0;
implFlags |= MethodImplAttributes.Runtime |
MethodImplAttributes.InternalCall;
break;
case MethodDefTreatment.Implementation:
// CLR implemenation class. Needs no adjustment
break;
case MethodDefTreatment.HiddenImpl:
// Implements a hidden WinMD interface
flags &= ~MethodAttributes.MemberAccessMask;
flags |= MethodAttributes.Private;
goto case MethodDefTreatment.Other;
case MethodDefTreatment.Other:
// All other cases
rva = 0;
implFlags |= MethodImplAttributes.Runtime |
MethodImplAttributes.InternalCall;
if (treatment.HasFlag(MethodDefTreatment.MarkAbstractFlag))
{
flags |= MethodAttributes.Abstract;
}
if (treatment.HasFlag(MethodDefTreatment.MarkPublicFlag))
{
flags &= ~MethodAttributes.MemberAccessMask;
flags |= MethodAttributes.Public;
}
if (treatment.HasFlag(MethodDefTreatment.MarkSpecialName))
{
flags |= MethodAttributes.SpecialName;
}
break;
case MethodDefTreatment.RenameToDisposeMethod:
rva = 0;
implFlags |= MethodImplAttributes.Runtime |
MethodImplAttributes.InternalCall;
name = "Dispose";
break;
}
flags |= MethodAttributes.HideBySig;
// Make WinRT delegate constructors public
if (flags.HasFlag(MethodAttributes.RTSpecialName) &&
flags.HasFlag(MethodAttributes.SpecialName) &&
MethodDefTreatment.Delegate == treatment &&
".ctor" == name)
{
flags = flags & ~(MethodAttributes.Private);
flags |= MethodAttributes.Public;
}
}
示例6: GetIcons
string[] GetIcons(string b, MethodAttributes a)
{
if (a.HasFlag(MethodAttributes.Private)) return GetIconsStub(b, "Private");
return GetIconsStub(b, string.Empty);
}