views:

69

answers:

3

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

+3  A: 

You have to include the namespace:

Type type = Type.GetType("YourNamespace.Criteria");

This will only work if your class is within the executing assembly. If it's from another assembly, you'll have to use a qualified assembly name. It looks like you should be able to just use the Type name prefixed with the namespace though in your scenario.

GenericTypeTea
Yes, I've included the namespace.One thing to mention is that I got this exception in my unit test, and haven't put the repository in useAll above code are in one assembly
Jun1st
+2  A: 

You should probably include the namespace as well:

namespace SomeNamespace
{
    class TheClass { } 
}


Type.GetType("TheClass"); // returns null
Type.GetType("SomeNamespace.TheClass"); // returns a Type object
Fredrik Mörk
Yes, I've included the namespace.One thing to mention is that I got this exception in my unit test. haven't put the repository in use
Jun1st
+2  A: 

I found the bug, it's a miserable typo error.

Jun1st