11using System.Linq.Expressions;
12using System.Reflection;
15using System.Reflection.Emit;
18using System.Collections.Generic;
49 internal static class DynamicMethodHandlerFactory
53 if (constructor ==
null)
54 throw new ArgumentNullException(
"constructor");
55 if (constructor.GetParameters().Length > 0)
58 Expression body = Expression.New(constructor);
60 if (constructor.DeclaringType.IsValueType())
62 if (constructor.DeclaringType.IsValueType)
65 body = Expression.Convert(body, typeof(
object));
74 throw new ArgumentNullException(
"method");
76 ParameterInfo[] pi = method.GetParameters();
77 var targetExpression = Expression.Parameter(typeof(
object));
78 var parametersExpression = Expression.Parameter(typeof(
object[]));
80 Expression[] callParametrs =
new Expression[pi.Length];
81 for (
int x = 0; x < pi.Length; x++)
85 Expression.ArrayIndex(
87 Expression.Constant(x)),
91 Expression instance = Expression.Convert(targetExpression, method.DeclaringType);
92 Expression body = pi.Length > 0
93 ? Expression.Call(instance, method, callParametrs)
94 : Expression.Call(instance, method);
96 if (method.ReturnType == typeof(
void))
98 var target = Expression.Label(typeof(
object));
99 var nullRef = Expression.Constant(
null);
100 body = Expression.Block(
102 Expression.Return(target, nullRef),
103 Expression.Label(target, nullRef));
106 else if (method.ReturnType.IsValueType())
108 else if (method.ReturnType.IsValueType)
111 body = Expression.Convert(body, typeof(
object));
117 parametersExpression);
124 if (property ==
null)
125 throw new ArgumentNullException(
"property");
127 if (!property.CanRead)
return null;
129 var target = Expression.Parameter(typeof(
object));
130 Expression body = Expression.Property(
131 Expression.Convert(target, property.DeclaringType),
135 if (property.PropertyType.IsValueType())
137 if (property.PropertyType.IsValueType)
140 body = Expression.Convert(body, typeof(
object));
147 return lambda.Compile();
152 if (property ==
null)
153 throw new ArgumentNullException(
"property");
155 if (!property.CanWrite)
return null;
157 var target = Expression.Parameter(typeof(
object));
158 var val = Expression.Parameter(typeof(
object));
160 Expression body = Expression.Assign(
162 Expression.Convert(target, property.DeclaringType),
164 Expression.Convert(val, property.PropertyType));
171 return lambda.Compile();
177 throw new ArgumentNullException(
"field");
179 var target = Expression.Parameter(typeof(
object));
180 Expression body = Expression.Field(
181 Expression.Convert(target, field.DeclaringType),
185 if (field.FieldType.IsValueType())
187 if (field.FieldType.IsValueType)
190 body = Expression.Convert(body, typeof(
object));
197 return lambda.Compile();
203 throw new ArgumentNullException(
"property");
205 var target = Expression.Parameter(typeof(
object));
206 var val = Expression.Parameter(typeof(
object));
208 Expression body = Expression.Assign(
210 Expression.Convert(target, field.DeclaringType),
212 Expression.Convert(val, field.FieldType));
219 return lambda.Compile();
222#if !NETFX_CORE && !IOS && !NETSTANDARD2_0 && !NET5_0
223 private static void EmitCastToReference(ILGenerator il, Type type)
225 if (type.IsValueType)
226 il.Emit(OpCodes.Unbox_Any, type);
228 il.Emit(OpCodes.Castclass, type);
A strongly-typed resource class, for looking up localized strings, etc.
static string ConstructorsWithParametersNotSupported
Looks up a localized string similar to Constructor with parameters are not supported.
delegate void DynamicMemberSetDelegate(object target, object arg)
Delegate for setting a value.
delegate object DynamicMethodDelegate(object target, object[] args)
Delegate for a dynamic method.
delegate object DynamicMemberGetDelegate(object target)
Delegate for getting a value.
delegate object DynamicCtorDelegate()
Delegate for a dynamic constructor method.