Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
void Main()
{
// GOAL: (object target) => (object)((T)target).PropertyName
// target => Convert(Convert(target, MyClass).MyProperty, Object)
Expression<Func<object, object>> expr = target => ((MyClass)target).MyProperty;
//expr.Dump();
// trarget => Convert(Convert(trarget, MyClass).MyProperty, Object)
var type = typeof(MyClass);
var propertyName = "MyProperty";
var target = Expression.Parameter(typeof(object), "trarget");
var lambda = Expression.Lambda<Func<object, object>>(
Expression.Convert(
Expression.PropertyOrField(
Expression.Convert(
target
,type)
, propertyName)
, typeof(object))
, target);
//lambda.Dump();
var func = lambda.Compile();
var test = new MyClass {MyProperty = 20};
func(test).Dump();
}
class MyClass
{
public int MyProperty { get; set; }
}
void Main()
{
var getFunc = CreateGetDelegate(typeof(MyClass), "MyProperty");
var test = new MyClass {MyProperty = 20};
getFunc(test).Dump();
}
// GOAL: (object target) => (object)((T)target).PropertyName
// Simulate code for validation: `Expression<Func<object, object>> expr = target => ((MyClass)target).MyProperty;`
// ExpressionResult: `target => Convert(Convert(target, MyClass).MyProperty, Object)`
static Func<object, object> CreateGetDelegate(Type type, string memberName)
{
var target = Expression.Parameter(typeof(object), "trarget");
var lambda = Expression.Lambda<Func<object, object>>(
Expression.Convert(
Expression.PropertyOrField(
Expression.Convert(
target
, type)
, memberName)
, typeof(object))
, target);
// trarget => Convert(Convert(trarget, MyClass).MyProperty, Object)
//lambda.Dump();
return lambda.Compile();
}
class MyClass
{
public int MyProperty { get; set; }
}
void Main()
{
var getFunc = CreateGetDelegate(typeof(MyClass), "MyProperty");
var test = new MyClass {MyProperty = 20};
getFunc(test).Dump();
var setFunc = CreateSetDelegate(typeof(MyClass), "MyProperty");
setFunc(test, 1000);
test.Dump();
}
// GOAL: (object target) => (object)((T)target).PropertyName
// Simulate code for validation: `Expression<Func<object, object>> expr = target => ((MyClass)target).MyProperty;`
// ExpressionResult: `target => Convert(Convert(target, MyClass).MyProperty, Object)`
static Func<object, object> CreateGetDelegate(Type type, string memberName)
{
var target = Expression.Parameter(typeof(object), "trarget");
var lambda = Expression.Lambda<Func<object, object>>(
Expression.Convert(
Expression.PropertyOrField(
Expression.Convert(
target
, type)
, memberName)
, typeof(object))
, target);
// trarget => Convert(Convert(trarget, MyClass).MyProperty, Object)
//lambda.Dump();
return lambda.Compile();
}
// GOAL: (object target, object value) => ((T)target).MyProperty = (U)value
// Simulate code for validation `Expression<Action<object, object>> expr = (target, value) => ((MyClass)target).MyProperty = (int)value;`
static Action<object, object> CreateSetDelegate(Type type, string memberName)
{
var target = Expression.Parameter(typeof(object), "trarget");
var value = Expression.Parameter(typeof(object), "value");
var left = Expression.PropertyOrField(
Expression.Convert(target, type), memberName);
var right = Expression.Convert(value, left.Type);
var lambda = Expression.Lambda<Action<object, object>>(Expression.Assign(left, right), target, value);
// (trarget, value) => (Convert(trarget, MyClass).MyProperty = Convert(value, Int32))
// lambda.Dump();
return lambda.Compile();
}
class MyClass
{
public int MyProperty { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.