本文整理汇总了C#中System.Reflection.Emit.MethodBuilder类的典型用法代码示例。如果您正苦于以下问题:C# MethodBuilder类的具体用法?C# MethodBuilder怎么用?C# MethodBuilder使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
MethodBuilder类属于System.Reflection.Emit命名空间,在下文中一共展示了MethodBuilder类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: InjectableConstructorArg
public InjectableConstructorArg(Type propertyType, string name, MethodBuilder getter, MethodBuilder setter)
{
PropertyType = propertyType;
Name = name;
Getter = getter;
Setter = setter;
}
示例2: GenCode
public override void GenCode(TypeBuilder tb, MethodBuilder mb, ILGenerator cg)
{
Type type = Type.EmptyTypes.GetType();
for (int i = 0; i < Compiler.Types.Count; i++)
if (Compiler.Types[i].name == Expr.ReturnType.Name)
type = Compiler.SearchType(Compiler.Types[i].typeArray);
if (assign)
{
assign = false;
LocalBuilder local = cg.DeclareLocal(type);
cg.Emit(OpCodes.Stloc, local);
Expr.GenCode(tb, mb, cg);//pone el array, luego el index y luego el valor para hacer stelem
Index.GenCode(tb, mb, cg);
cg.Emit(OpCodes.Ldloc, local);
cg.Emit(OpCodes.Stelem, type);
}
else
{
Expr.GenCode(tb, mb, cg);
Index.GenCode(tb, mb, cg);
cg.Emit(OpCodes.Ldelem, type);
}
}
示例3: Generate
public static void Generate(
TypeBuilder typeBuilder,
EventMetadata eventMetadata,
MethodBuilder addMethod,
MethodBuilder removeMethod,
MethodBuilder raiseMethod)
{
var @event = typeBuilder.DefineEvent(
eventMetadata.Name,
eventMetadata.EventAttributes,
eventMetadata.EventHandlerType);
if (addMethod != null)
{
@event.SetAddOnMethod(addMethod);
}
if (removeMethod != null)
{
@event.SetRemoveOnMethod(removeMethod);
}
if (raiseMethod != null)
{
@event.SetRaiseMethod(raiseMethod);
}
}
示例4: EmitEmptyImplementation
private static void EmitEmptyImplementation( MethodBuilder vM, Type returnType, ParameterInfo[] parameters )
{
ILGenerator gVM = vM.GetILGenerator();
for( int i = 0; i < parameters.Length; ++i )
{
// DefineParameter use 0 for the return parameter.
ParameterInfo param = parameters[i];
vM.DefineParameter( i + 1, param.Attributes, param.Name );
if( param.IsOut )
{
Debug.Assert( param.ParameterType.IsByRef, "'Out' is just an attribute on 'by ref' parameters (unfortunate for covariance support)." );
gVM.StoreDefaultValueForOutParameter( param );
}
}
if( returnType != typeof( void ) )
{
if( returnType.GetTypeInfo().IsValueType )
{
LocalBuilder retValue = gVM.DeclareLocal( returnType );
gVM.Emit( OpCodes.Ldloca_S, retValue.LocalIndex );
gVM.Emit( OpCodes.Initobj, returnType );
gVM.LdLoc( retValue );
}
else
{
gVM.Emit( OpCodes.Ldnull );
}
}
gVM.Emit( OpCodes.Ret );
}
示例5: CreateMethodBuilder
private void CreateMethodBuilder()
{
methodBuilder = typeBuilder.DefineMethod(
"DynamicMethod", MethodAttributes.Public | MethodAttributes.Static, typeof(object), new Type[] { typeof(object), typeof(object[]) });
methodBuilder.InitLocals = true;
ilGenerator = methodBuilder.GetILGenerator();
}
示例6: CreateMethodBuilder
private void CreateMethodBuilder(Type returnType, Type[] parameterTypes)
{
methodBuilder = typeBuilder.DefineMethod(
"DynamicMethod", MethodAttributes.Public | MethodAttributes.Static, returnType, parameterTypes);
methodBuilder.InitLocals = true;
emitter = new Emitter(methodBuilder.GetILGenerator(), parameterTypes);
}
示例7: CreateContinueMethod
private void CreateContinueMethod(MethodInfo method, MethodBuilder continueMethod, int position, Type[] parameters, int count)
{
var generator = continueMethod.GetILGenerator();
// Store array in temp0
generator.DeclareLocal(typeof (object[]));
generator.LoadArgument(1);
generator.StashTemporaryVariable(0);
// Push backing object onto the stack
generator.LoadField(backingObject);
// Push each of the arguments passed in onto the stack
for (int i = 0; i < count; i++)
{
generator.LoadTemporaryVariable(0);
generator.LoadInteger(i);
generator.Emit(OpCodes.Ldelem_Ref);
generator.MaybeUnBox(parameters[i]);
}
// Delegate to backing object
generator.Execute(method);
// Convert the returned value to an object
if (method.ReturnType == typeof(void))
generator.LoadNull();
else
generator.MaybeBox(method.ReturnType);
// Return
generator.Emit(OpCodes.Ret);
}
示例8: GenerateFunction
private void GenerateFunction(TypeBuilder typeBuilder, out MethodBuilder methodBuilder)
{
methodBuilder = null;
ProgramDeclBlock pdb = this.expr as ProgramDeclBlock;
if (pdb != null)
{
foreach (var function in pdb.Statements)
{
var functionDecl = function as BlockDeclExpr;
if (functionDecl != null)
{
bool isMain = functionDecl.IsMain;
var parameters = functionDecl.Parameters;
Expr mainFunctionParameters = null;
if (isMain)
{
mainFunctionParameters =
GenerateMainMethod(
typeBuilder,
functionDecl,
parameters,
mainFunctionParameters,
out methodBuilder);
}
}
}
}
}
示例9: IncLevel
public void IncLevel(int i, string procId)
{
Debug.WriteLine(String.Format("Generator: IncLevel({0},{1})", i, procId));
if(i > 0)
currentMethodBuilder = currentType.DefineMethod(procId, MethodAttributes.Static, typeof(void), System.Type.EmptyTypes);
}
示例10: EmitMethodBody
private static void EmitMethodBody (MethodBuilder methodBuilder, MethodInfo methodInfo, String message)
{
Type returnType = methodInfo.ReturnType;
ILGenerator generator = methodBuilder.GetILGenerator ();
ParameterInfo[] parameterInfos = methodInfo.GetParameters ();
if (returnType == typeof(void)) {
MethodInfo sendMessageInvoker = EmitInfos.OBJECTIVECRUNTIME_SENDMESSAGE_VOID_ARGS;
// Makes the actual call
generator.Emit (OpCodes.Ldarg_0);
generator.Emit (OpCodes.Ldstr, message);
EmitParametersAsArrayOnStack (generator, parameterInfos);
generator.Emit (OpCodes.Call, sendMessageInvoker);
generator.Emit (OpCodes.Ret);
} else {
MethodInfo sendMessageInvoker = EmitInfos.OBJECTIVECRUNTIME_SENDMESSAGE_ARGS_GENERIC.MakeGenericMethod (returnType);
// Makes the actual call
generator.Emit (OpCodes.Ldarg_0);
generator.Emit (OpCodes.Ldstr, message);
EmitParametersAsArrayOnStack (generator, parameterInfos);
generator.Emit (OpCodes.Call, sendMessageInvoker);
generator.Emit (OpCodes.Ret);
}
}
示例11: SetupParameters
public void SetupParameters(MethodBuilder methodBuilder)
{
if (methodToOverride.IsGenericMethod)
{
Type[] genericArguments = methodToOverride.GetGenericArguments();
string[] names = genericArguments
.Select(t => t.Name)
.ToArray();
GenericTypeParameterBuilder[] builders = methodBuilder.DefineGenericParameters(names);
for (int i = 0; i < genericArguments.Length; ++i)
{
builders[i].SetGenericParameterAttributes(genericArguments[i].GenericParameterAttributes);
var constraintTypes = genericArguments[i].GetGenericParameterConstraints();
builders[i].SetInterfaceConstraints(constraintTypes.Where(t => t.IsInterface).ToArray());
foreach (Type type in constraintTypes.Where(t => !t.IsInterface))
{
builders[i].SetBaseTypeConstraint(type);
}
}
for(int i = 0; i < genericArguments.Length; ++i)
{
genericMethodParameters.Add(new KeyValuePair<Type, Type>(genericArguments[i], builders[i].UnderlyingSystemType));
}
}
}
示例12: EmitBody
private static void EmitBody(MethodBuilder mb, Body body)
{
foreach (var st in body.Statements)
{
EmitStatement(mb, st);
}
}
示例13: TypeBuilder
internal TypeBuilder(string szName, int genParamPos, MethodBuilder declMeth)
{
this.m_declMeth = declMeth;
this.m_DeclaringType = this.m_declMeth.GetTypeBuilder();
this.m_module = declMeth.GetModuleBuilder();
this.InitAsGenericParam(szName, genParamPos);
}
示例14: RewriteInto
public static IILRewriterContext RewriteInto(this MethodBase src, MethodBuilder dest,
Func<IILOp, IILRewriteControl, IILRewriteControl> logic)
{
var rewriter = new ILRewriter(src, dest, logic);
rewriter.DoRewrite();
return rewriter;
}
示例15: CloneGenericMethodArguments
protected static GenericTypeParameterBuilder[] CloneGenericMethodArguments(MethodInfo source, MethodBuilder destination)
{
var genericArgs = source.GetGenericArguments();
if (genericArgs.Length == 0)
{
return new GenericTypeParameterBuilder[0];
}
var names = new string[genericArgs.Length];
for (var i = 0; i < genericArgs.Length; i++)
{
names[i] = genericArgs[i].Name;
}
var genericTypeParameterBuilders = destination.DefineGenericParameters(names);
for (var i = 0; i < genericTypeParameterBuilders.Length; i++)
{
var genericArgConstraints = genericArgs[i].GetGenericParameterConstraints();
for (var j = 0; j < genericArgConstraints.Length; j++)
{
genericTypeParameterBuilders[i].SetBaseTypeConstraint(genericArgConstraints[j]);
}
}
return genericTypeParameterBuilders;
}