Expression<Func< string , int >> ex = s => s.Replace( "x" , "yy" ).Length*2;
* This source code was highlighted with Source Code Highlighter .
ParameterExpression CS$0$0000;
Expression<Func< string , int >> ex = Expression.Lambda<Func< string , int >>(Expression.Multiply(Expression.Property(Expression.Call(CS$0$0000 = Expression.Parameter( typeof ( string ), "s" ), (MethodInfo) methodof( string .Replace), new Expression[] { Expression.Constant( "x" , typeof ( string )), Expression.Constant( "yy" , typeof ( string )) }), (MethodInfo) methodof( string .get_Length)), Expression.Constant(2, typeof ( int ))), new ParameterExpression[] { CS$0$0000 });
* This source code was highlighted with Source Code Highlighter .
internal class ReflectionCreator<T>:ICreator<T>
{
private readonly List <PropertyInfo> _infos;
public ReflectionCreator()
{
_infos = new List <PropertyInfo>( typeof (T).GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.SetProperty));
}
public T Create(Dictionary< string , object > props)
{
var newObject = Activator.CreateInstance<T>();
foreach ( var propertyInfo in _infos)
{
object value ;
if (props.TryGetValue(propertyInfo.Name, out value ))
{
propertyInfo.SetValue(newObject, value , null );
}
}
return newObject;
}
}
* This source code was highlighted with Source Code Highlighter .
class Foo
{
public string Name { get ; set ; }
public int Value { get ; set ; }
}
* This source code was highlighted with Source Code Highlighter .
Func<Dictionary< string , object >, Foo> fooCreator =
d => new Foo
{
Name = d.GetValue< string >( "Name" ),
Value = d.GetValue< int >( "Value" )
};
* This source code was highlighted with Source Code Highlighter .
static class DictionaryExtension
{
public static TType GetValue<TType>( this Dictionary< string , object > d, string name)
{
object value ;
return d.TryGetValue(name, out value ) ? (TType) value : default (TType);
}
}
* This source code was highlighted with Source Code Highlighter .
* This source code was highlighted with Source Code Highlighter .
- ParameterExpression CS0 = Expression.Parameter ( typeof (Dictionary < string , object >), "d" );
- var fooCreator = Expression.Lambda <Func <Dictionary < string , object >, Foo >>
- (
- Expression.MemberInit
- (
- Expression.New
- (
- (ConstructorInfo) methodof (Foo..ctor),
- new Expression [0]
- ),
- new MemberBinding []
- {
- Expression.Bind
- (
- (MethodInfo) methodof (Foo.set_Name),
- Expression.Call
- (
- null
- (MethodInfo) methodof (DictionaryExtension.GetValue),
- new Expression []
- {
- CS0,
- Expression.Constant ( "Name" , typeof ( string ))
- }
- )
- ),
- Expression.Bind
- (
- (MethodInfo) methodof (Foo.set_Value),
- Expression.Call
- (
- null
- (MethodInfo) methodof (DictionaryExtension.GetValue),
- new Expression []
- {
- CS0,
- Expression.Constant ( "Value" , typeof ( string ))
- }
- )
- )
- }
- ),
- new ParameterExpression [] {CS0}
- );
class ExpressionCreator<T> : ICreator<T>
{
private readonly Func<Dictionary< string , object >, T> _creator;
public ExpressionCreator()
{
var type = typeof (T);
var newExpression = Expression.New(type);
var dictParam = Expression.Parameter( typeof (Dictionary< string , object >), "d" );
var list = new List <MemberBinding>();
var propertyInfos = type.GetProperties(BindingFlags.Instance |
BindingFlags.Public |
BindingFlags.SetProperty);
foreach ( var propertyInfo in propertyInfos)
{
Expression call = Expression.Call(
typeof (DictionaryExtension),
"GetValue" , new [] {propertyInfo.PropertyType},
new Expression[]
{
dictParam,
Expression.Constant(propertyInfo.Name)
});
MemberBinding mb = Expression.Bind(propertyInfo.GetSetMethod(), call);
list.Add(mb);
}
var ex = Expression.Lambda<Func<Dictionary< string , object >, T>>(
Expression.MemberInit(newExpression, list),
new [] {dictParam});
_creator = ex.Compile();
}
public T Create(Dictionary< string , object > props)
{
return _creator(props);
}
}
* This source code was highlighted with Source Code Highlighter .
internal class DirectCreator : ICreator<Foo>
{
public Foo Create(Dictionary< string , object > props)
{
return new Foo
{
Name = props.GetValue< string >( "Name" ),
Value = props.GetValue< int >( "Value" )
};
}
}
* This source code was highlighted with Source Code Highlighter .
Source: https://habr.com/ru/post/83169/
All Articles