public ConstructorEmitter CreateConstructor(ArgumentReference[] baseCtorArguments, AbstractTypeEmitter invocation) { var arguments = GetArguments(baseCtorArguments); var constructor = invocation.CreateConstructor(arguments); var delegateField = invocation.CreateField("delegate", delegateType); constructor.CodeBuilder.AddStatement(new AssignStatement(delegateField, new ReferenceExpression(arguments[0]))); return constructor; }
public static ArgumentReference[] ConvertToArgumentReference(ParameterInfo[] args) { var arguments = new ArgumentReference[args.Length]; for (var i = 0; i < args.Length; ++i) { arguments[i] = new ArgumentReference(args[i].ParameterType); } return arguments; }
public static Expression[] ConvertArgumentReferenceToExpression(ArgumentReference[] args) { var expressions = new Expression[args.Length]; for (var i = 0; i < args.Length; ++i) { expressions[i] = args[i].ToExpression(); } return expressions; }
private ArgumentReference[] GetArguments(ArgumentReference[] baseCtorArguments) { var arguments = new ArgumentReference[baseCtorArguments.Length + 1]; arguments[0] = new ArgumentReference(delegateType); baseCtorArguments.CopyTo(arguments, 1); return arguments; }
private bool HasByRefArguments(ArgumentReference[] arguments) { for (int i = 0; i < arguments.Length; i++ ) { if (arguments[i].Type.IsByRef) { return true; } } return false; }
public AssignArgumentStatement(ArgumentReference argument, Expression expression) { this.argument = argument; this.expression = expression; }
public ConstructorEmitter CreateConstructor(ArgumentReference[] baseCtorArguments, AbstractTypeEmitter invocation) { return invocation.CreateConstructor(baseCtorArguments); }
protected void GenerateConstructor(ClassEmitter emitter, ConstructorInfo baseConstructor, ProxyConstructorImplementation impl, params FieldReference[] fields) { if (impl == ProxyConstructorImplementation.SkipConstructor) return; ArgumentReference[] args; ParameterInfo[] baseConstructorParams = null; if (baseConstructor != null) { baseConstructorParams = baseConstructor.GetParameters(); } if (baseConstructorParams != null && baseConstructorParams.Length != 0) { args = new ArgumentReference[fields.Length + baseConstructorParams.Length]; var offset = fields.Length; for (var i = offset; i < offset + baseConstructorParams.Length; i++) { var paramInfo = baseConstructorParams[i - offset]; args[i] = new ArgumentReference(paramInfo.ParameterType, paramInfo.DefaultValue); } } else { args = new ArgumentReference[fields.Length]; } for (var i = 0; i < fields.Length; i++) { args[i] = new ArgumentReference(fields[i].Reference.FieldType); } var constructor = emitter.CreateConstructor(args); if (baseConstructorParams != null && baseConstructorParams.Length != 0) { var last = baseConstructorParams.Last(); if (last.ParameterType.IsArray && last.HasAttribute<ParamArrayAttribute>()) { var parameter = constructor.ConstructorBuilder.DefineParameter(args.Length, ParameterAttributes.None, last.Name); var builder = AttributeUtil.CreateBuilder<ParamArrayAttribute>(); parameter.SetCustomAttribute(builder); } } for (var i = 0; i < fields.Length; i++) { constructor.CodeBuilder.AddStatement(new AssignStatement(fields[i], args[i].ToExpression())); } // Invoke base constructor if (impl == ProxyConstructorImplementation.CallBase) { if (baseConstructor != null) { Debug.Assert(baseConstructorParams != null); var slice = new ArgumentReference[baseConstructorParams.Length]; Array.Copy(args, fields.Length, slice, 0, baseConstructorParams.Length); constructor.CodeBuilder.InvokeBaseConstructor(baseConstructor, slice); } else { constructor.CodeBuilder.InvokeBaseConstructor(); } } constructor.CodeBuilder.AddStatement(new ReturnStatement()); }
public static Type[] InitializeAndConvert(ArgumentReference[] args) { var types = new Type[args.Length]; for (var i = 0; i < args.Length; ++i) { args[i].Position = i + 1; types[i] = args[i].Type; } return types; }
public static void InitializeArgumentsByPosition(ArgumentReference[] args, bool isStatic) { var offset = isStatic ? 0 : 1; for (var i = 0; i < args.Length; ++i) { args[i].Position = i + offset; } }