Hey
We are about to do some reflector in my company.
I need a FactoryClass that can convert an IDictionary to a obj, by doing a match on properties and dict keys.
I found:
http://stackoverflow.com/questions/1273589/dynamic-object-property-populator-without-reflection
This code can do what I want, and I want to use this code, because it is done by using the basic of dotNET without using others extensions.
public class Populator<T>
{
private delegate T Load(Dictionary<string, object> properties);
private Load _handler;
private Populator() { }
public T Build(Dictionary<string, object> properties)
{
return _handler(properties);
}
public static Populator<T> CreateBuilder(Dictionary<string, object> properties)
{
//private static readonly MethodInfo getValueMethod = typeof(IDataRecord).GetMethod("get_Item", new [] { typeof(int) });
//private static readonly MethodInfo isDBNullMethod = typeof(IDataRecord).GetMethod("IsDBNull", new [] { typeof(int) });
Populator<T> dynamicBuilder = new Populator<T>();
...
When I tested this code, I got an error.
public ICollection<object> GetKeys(IDictionary<object, object> products)
{
IDictionary<object, object> product = (IDictionary<object, object>)products.ElementAt(0).Value;
Dictionary<string, object> p = new Dictionary<string, object>();
foreach (KeyValuePair<object, object> item in product)
{
p.Add(item.Key.ToString(), item.Value);
}
Populator<ProductTest> builder = Populator<ProductTest>.CreateBuilder(p);
ProductTest obj = builder.Build(p); // error here
return null;
}
I got and error here
public class Populator<T>
{
private delegate T Load(Dictionary<string, object> properties);
private Load _handler;
private Populator() { }
public T Build(Dictionary<string, object> properties)
{
return _handler(properties); // Error: JIT Compiler encountered an internal limitation.
}
Wy question is then why, and how to solve it ? There is nothing in extra in the stacktrace.
// dennis