I have a class named "Criteria", a Repository named CriteriaReposiroty.
CriteriaReposiroty reads a xml file and returns a list of Criteria
public static Func<XElement, Criteria> BuildObjectFromXElement(string typeName)
{
ParameterExpression element = Expression.Parameter(typeof(XElement), "element");
List<MemberBinding> bindings = new List<MemberBinding>();
foreach (PropertyInfo prop in GetWritableProperties(Type.GetType(typeName)))
{
MethodCallExpression propertyValue = Expression.Call(
typeof(XElementAttributeHelper).GetMethod("GetAttributeValue").MakeGenericMethod(prop.PropertyType), element,
Expression.Constant(prop.Name));
MemberBinding binding = Expression.Bind(prop, propertyValue);
bindings.Add(binding);
}
Expression initializer = Expression.MemberInit(Expression.New(Type.GetType(typeName)), bindings);
return Expression.Lambda<Func<XElement, Criteria>>(initializer, element).Compile();
}
public static IEnumerable<PropertyInfo> GetWritableProperties(Type t)
{
foreach (PropertyInfo property in t.GetProperties())
{
if (property.CanWrite)
yield return property;
}
}
public static T GetAttributeValue<T>(this XElement element, string attrName)
{
foreach (XAttribute attribute in element.Attributes())
{
if (string.Compare(attribute.Name.ToString(), attrName, true, CultureInfo.InvariantCulture) == 0)
{
if (string.IsNullOrEmpty(attribute.Value))
return default(T);
TypeConverter converter = TypeDescriptor.GetConverter(typeof(T));
return (T)converter.ConvertFromString(attribute.Value);
}
}
return default(T);
}
when I call Type.GetType("Criteria"), it always returns null.
can anyone help?
Thanks in Advance