本文整理汇总了C#中MethodBuilder类的典型用法代码示例。如果您正苦于以下问题:C# MethodBuilder类的具体用法?C# MethodBuilder怎么用?C# MethodBuilder使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
MethodBuilder类属于命名空间,在下文中一共展示了MethodBuilder类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ReturnParameter
// TODO: merge method and mb
public ReturnParameter (MemberCore method, MethodBuilder mb, Location location)
{
this.method = method;
try {
builder = mb.DefineParameter (0, ParameterAttributes.None, "");
}
catch (ArgumentOutOfRangeException) {
method.Compiler.Report.RuntimeMissingSupport (location, "custom attributes on the return type");
}
}
示例2: VerifyMethodInfo
private void VerifyMethodInfo(MethodInfo methodInfo, MethodBuilder builder, Type returnType)
{
if (methodInfo == null)
{
Assert.Null(builder);
}
else
{
Assert.Equal(methodInfo.Name, builder.Name);
Assert.Equal(methodInfo.ReturnType, returnType);
}
}
示例3: EmitTestEvents
static void EmitTestEvents (TypeBuilder genericFoo) {
MethodBuilder mb = genericFoo.DefineMethod ("TestEvents", MethodAttributes.Public, typeof (void), null);
ILGenerator il = mb.GetILGenerator ();
for (int i = 0; i < 20; ++i)
il.Emit (OpCodes.Nop);
il.Emit (OpCodes.Ldarg_0);
il.Emit (OpCodes.Ldnull);
il.Emit (OpCodes.Callvirt, targetMethod);
il.Emit (OpCodes.Ret);
testEvents = mb;
}
示例4: EmitTargetMethod
static void EmitTargetMethod (TypeBuilder genericFoo) {
MethodBuilder mb = genericFoo.DefineMethod ("TargetMethod", MethodAttributes.Public, typeof (void), new Type[] {typeof (object) });
ILGenerator il = mb.GetILGenerator ();
for (int i = 0; i < 20; ++i)
il.Emit (OpCodes.Nop);
il.Emit (OpCodes.Ldtoken, genericArgs [0]);
il.Emit (OpCodes.Call, typeof (Type).GetMethod ("GetTypeFromHandle"));
il.Emit (OpCodes.Call, typeof (Console).GetMethod ("WriteLine", new Type[] { typeof (object) }));
il.Emit (OpCodes.Ret);
targetMethod = mb;
}
示例5: DefaultPropertyBuilder
/// <summary>
/// Constructor
/// </summary>
/// <param name="typeBuilder">Type builder</param>
/// <param name="name">Name of the property</param>
/// <param name="attributes">Attributes for the property (public, private, etc.)</param>
/// <param name="getMethodAttributes">Get method attributes</param>
/// <param name="setMethodAttributes">Set method attributes</param>
/// <param name="propertyType">Property type for the property</param>
/// <param name="parameters">Parameter types for the property</param>
public DefaultPropertyBuilder(TypeBuilder typeBuilder, string name,
PropertyAttributes attributes, MethodAttributes getMethodAttributes,
MethodAttributes setMethodAttributes,
Type propertyType, IEnumerable<Type> parameters)
{
if (typeBuilder == null)
throw new ArgumentNullException("typeBuilder");
if (string.IsNullOrEmpty(name))
throw new ArgumentNullException("name");
Name = name;
Type = typeBuilder;
Attributes = attributes;
GetMethodAttributes = getMethodAttributes;
SetMethodAttributes = setMethodAttributes;
DataType = propertyType;
Parameters = new List<ParameterBuilder>();
if (parameters != null)
{
int x = 1;
foreach (var parameter in parameters)
{
Parameters.Add(new ParameterBuilder(parameter, x));
++x;
}
}
Field = new FieldBuilder(Type, "_" + name + "field", propertyType, FieldAttributes.Private);
Builder = Type.Builder.DefineProperty(name, attributes, propertyType,
(parameters != null && parameters.Count() > 0)
? parameters.ToArray()
: System.Type.EmptyTypes);
GetMethod = new MethodBuilder(Type, "get_" + name, getMethodAttributes, parameters, propertyType);
GetMethod.Generator.Emit(OpCodes.Ldarg_0);
GetMethod.Generator.Emit(OpCodes.Ldfld, Field.Builder);
GetMethod.Generator.Emit(OpCodes.Ret);
var setParameters = new List<Type>();
if (parameters != null)
{
setParameters.AddRange(parameters);
}
setParameters.Add(propertyType);
SetMethod = new MethodBuilder(Type, "set_" + name, setMethodAttributes, setParameters, typeof (void));
SetMethod.Generator.Emit(OpCodes.Ldarg_0);
SetMethod.Generator.Emit(OpCodes.Ldarg_1);
SetMethod.Generator.Emit(OpCodes.Stfld, Field.Builder);
SetMethod.Generator.Emit(OpCodes.Ret);
Builder.SetGetMethod(GetMethod.Builder);
Builder.SetSetMethod(SetMethod.Builder);
}
示例6: VerifyGenericArguments
private static void VerifyGenericArguments(MethodBuilder method, GenericTypeParameterBuilder[] expected)
{
Type[] genericArguments = method.GetGenericArguments();
if (expected == null)
{
Assert.Null(genericArguments);
}
else
{
Assert.Equal(expected.Length, genericArguments.Length);
for (int i = 0; i < genericArguments.Length; ++i)
{
Assert.True(expected[i].Equals(genericArguments[i]));
}
}
}
示例7: DefaultPropertyBuilder
/// <summary>
/// Constructor
/// </summary>
/// <param name="TypeBuilder">Type builder</param>
/// <param name="Name">Name of the property</param>
/// <param name="Attributes">Attributes for the property (public, private, etc.)</param>
/// <param name="GetMethodAttributes">Get method attributes</param>
/// <param name="SetMethodAttributes">Set method attributes</param>
/// <param name="PropertyType">Property type for the property</param>
/// <param name="Parameters">Parameter types for the property</param>
public DefaultPropertyBuilder(TypeBuilder TypeBuilder, string Name,
PropertyAttributes Attributes, MethodAttributes GetMethodAttributes,
MethodAttributes SetMethodAttributes,
Type PropertyType, List<Type> Parameters)
: base()
{
if (TypeBuilder == null)
throw new ArgumentNullException("TypeBuilder");
if (string.IsNullOrEmpty(Name))
throw new ArgumentNullException("Name");
this.Name = Name;
this.Type = TypeBuilder;
this.Attributes = Attributes;
this.GetMethodAttributes = GetMethodAttributes;
this.SetMethodAttributes = SetMethodAttributes;
this.DataType = PropertyType;
this.Parameters = new List<ParameterBuilder>();
if (Parameters != null)
{
int x = 1;
foreach (Type Parameter in Parameters)
{
this.Parameters.Add(new ParameterBuilder(Parameter, x));
++x;
}
}
Field = new FieldBuilder(Type, "_" + Name + "field", PropertyType, FieldAttributes.Private);
Builder = Type.Builder.DefineProperty(Name, Attributes, PropertyType,
(Parameters != null && Parameters.Count > 0) ? Parameters.ToArray() : System.Type.EmptyTypes);
GetMethod = new MethodBuilder(Type, "get_" + Name, GetMethodAttributes, Parameters, PropertyType);
GetMethod.Generator.Emit(OpCodes.Ldarg_0);
GetMethod.Generator.Emit(OpCodes.Ldfld, Field.Builder);
GetMethod.Generator.Emit(OpCodes.Ret);
List<Type> SetParameters = new List<System.Type>();
if (Parameters != null)
{
SetParameters.AddRange(Parameters);
}
SetParameters.Add(PropertyType);
SetMethod = new MethodBuilder(Type, "set_" + Name, SetMethodAttributes, SetParameters, typeof(void));
SetMethod.Generator.Emit(OpCodes.Ldarg_0);
SetMethod.Generator.Emit(OpCodes.Ldarg_1);
SetMethod.Generator.Emit(OpCodes.Stfld, Field.Builder);
SetMethod.Generator.Emit(OpCodes.Ret);
Builder.SetGetMethod(GetMethod.Builder);
Builder.SetSetMethod(SetMethod.Builder);
}
示例8: PropertyBuilder
/// <summary>
/// Constructor
/// </summary>
/// <param name="TypeBuilder">Type builder</param>
/// <param name="Name">Name of the property</param>
/// <param name="Attributes">Attributes for the property (public, private, etc.)</param>
/// <param name="GetMethodAttributes">Get method attributes</param>
/// <param name="SetMethodAttributes">Set method attributes</param>
/// <param name="PropertyType">Property type for the property</param>
/// <param name="Parameters">Parameter types for the property</param>
public PropertyBuilder(TypeBuilder TypeBuilder,
string Name,
PropertyAttributes Attributes,
MethodAttributes GetMethodAttributes,
MethodAttributes SetMethodAttributes,
Type PropertyType,
IEnumerable<Type> Parameters)
: base()
{
Contract.Requires<ArgumentNullException>(TypeBuilder!=null,"TypeBuilder");
Contract.Requires<ArgumentNullException>(!string.IsNullOrEmpty(Name),"Name");
this.Name = Name;
this.Type = TypeBuilder;
this.Attributes = Attributes;
this.GetMethodAttributes = GetMethodAttributes;
this.SetMethodAttributes = SetMethodAttributes;
this.DataType = PropertyType;
this.Parameters = new List<ParameterBuilder>();
if (Parameters != null)
{
int x = 1;
foreach (Type Parameter in Parameters)
{
this.Parameters.Add(new ParameterBuilder(Parameter, x));
++x;
}
}
Builder = Type.Builder.DefineProperty(Name, Attributes, PropertyType,
(Parameters != null && Parameters.Count() > 0) ? Parameters.ToArray() : System.Type.EmptyTypes);
GetMethod = new MethodBuilder(Type, "get_" + Name, GetMethodAttributes, Parameters, PropertyType);
List<Type> SetParameters = new List<System.Type>();
if (Parameters != null)
SetParameters.AddRange(Parameters);
SetParameters.Add(PropertyType);
SetMethod = new MethodBuilder(Type, "set_" + Name, SetMethodAttributes, SetParameters, typeof(void));
Builder.SetGetMethod(GetMethod.Builder);
Builder.SetSetMethod(SetMethod.Builder);
}
示例9: initialize
void initialize(MethodBuilder methodBuilder, TypeBuilder lambdaScope) {
this.methods.clear();
this.ParametersUsedInLambdas.clear();
this.ParametersUsedInLambda.clear();
this.CatchVariables.clear();
this.TreeLocals.clear();
this.TreeLabels.clear();
this.LocalFields.clear();
this.Labels.clear();
this.methods.add(methodBuilder);
this.LambdaScope = lambdaScope;
this.Generator = methodBuilder.CodeGenerator;
this.destructor = methodBuilder.Name.equals("finalize") && !methodBuilder.Parameters.any();
this.IsLambdaScopeUsed = false;
this.IsLambdaScopeInitialized = false;
this.IsLambdaScopeThisInitialized = false;
this.IsBuildingString = false;
this.foreachStatement = 0;
this.YieldCount = 0;
this.stringSwitch = 0;
this.PreviousLineNumber = 0;
this.generatedLocal = 0;
}
示例10: enterLambdaMethod
void enterLambdaMethod(MethodBuilder methodBuilder) {
context.MemberResolver.enterMethod(methodBuilder, true);
methods.add(methodBuilder);
returnTypes.add(new ArrayList<TypeInfo>());
}
示例11: enterMethod
void enterMethod(MethodBuilder methodBuilder) {
context.MemberResolver.enterMethod(methodBuilder);
this.IsStatic = methodBuilder.IsStatic;
methods.add(methodBuilder);
this.YieldCount = 0;
}
示例12: Define
public override bool Define ()
{
if (!base.Define ())
return false;
if (!CheckBase ())
return false;
MemberKind kind;
if (this is Operator)
kind = MemberKind.Operator;
else if (this is Destructor)
kind = MemberKind.Destructor;
else
kind = MemberKind.Method;
if (IsPartialDefinition) {
caching_flags &= ~Flags.Excluded_Undetected;
caching_flags |= Flags.Excluded;
// Add to member cache only when a partial method implementation has not been found yet
if ((caching_flags & Flags.PartialDefinitionExists) == 0) {
// MethodBase mb = new PartialMethodDefinitionInfo (this);
spec = new MethodSpec (kind, Parent.Definition, this, ReturnType, null, parameters, ModFlags);
if (MemberName.Arity > 0) {
spec.IsGeneric = true;
// TODO: Have to move DefineMethod after Define (ideally to Emit)
throw new NotImplementedException ("Generic partial methods");
}
Parent.MemberCache.AddMember (spec);
}
return true;
}
MethodData = new MethodData (
this, ModFlags, flags, this, MethodBuilder, base_method);
if (!MethodData.Define (Parent.PartialContainer, GetFullName (MemberName)))
return false;
MethodBuilder = MethodData.MethodBuilder;
spec = new MethodSpec (kind, Parent.Definition, this, ReturnType, MethodBuilder, parameters, ModFlags);
if (MemberName.Arity > 0)
spec.IsGeneric = true;
Parent.MemberCache.AddMember (this, MethodBuilder.Name, spec);
return true;
}
示例13: Apply
internal override void Apply(ClassLoaderWrapper loader, MethodBuilder mb, object annotation)
{
Annotation annot = type.Annotation;
foreach (object ann in UnwrapArray(annotation))
{
annot.Apply(loader, mb, ann);
}
}
示例14: ApplyAttributes
public virtual void ApplyAttributes (MethodBuilder mb, ConstructorBuilder cb, int index, PredefinedAttributes pa)
{
if (builder != null)
throw new InternalErrorException ("builder already exists");
var pattrs = ParametersCompiled.GetParameterAttribute (modFlags);
if (HasOptionalExpression)
pattrs |= ParameterAttributes.Optional;
if (mb == null)
builder = cb.DefineParameter (index, pattrs, Name);
else
builder = mb.DefineParameter (index, pattrs, Name);
if (OptAttributes != null)
OptAttributes.Emit ();
if (HasDefaultValue) {
//
// Emit constant values for true constants only, the other
// constant-like expressions will rely on default value expression
//
var def_value = DefaultValue;
Constant c = def_value != null ? def_value.Child as Constant : default_expr as Constant;
if (c != null) {
if (c.Type.BuiltinType == BuiltinTypeSpec.Type.Decimal) {
pa.DecimalConstant.EmitAttribute (builder, (decimal) c.GetValue (), c.Location);
} else {
builder.SetConstant (c.GetValue ());
}
} else if (default_expr.Type.IsStruct) {
//
// Handles special case where default expression is used with value-type
//
// void Foo (S s = default (S)) {}
//
builder.SetConstant (null);
}
}
if (parameter_type != null) {
if (parameter_type.BuiltinType == BuiltinTypeSpec.Type.Dynamic) {
pa.Dynamic.EmitAttribute (builder);
} else if (parameter_type.HasDynamicElement) {
pa.Dynamic.EmitAttribute (builder, parameter_type, Location);
}
}
}
示例15: ApplyReturnValue
internal override void ApplyReturnValue(ClassLoaderWrapper loader, MethodBuilder mb, ref ParameterBuilder pb, object annotation)
{
// TODO make sure the descriptor is correct
Annotation ann = type.Annotation;
object[] arr = (object[])annotation;
for (int i = 2; i < arr.Length; i += 2)
{
if ("value".Equals(arr[i]))
{
if (pb == null)
{
pb = mb.DefineParameter(0, ParameterAttributes.None, null);
}
object[] value = (object[])arr[i + 1];
if (value[0].Equals(AnnotationDefaultAttribute.TAG_ANNOTATION))
{
ann.Apply(loader, pb, value);
}
else
{
for (int j = 1; j < value.Length; j++)
{
ann.Apply(loader, pb, value[j]);
}
}
break;
}
}
}