Wednesday, August 3, 2011

One of the many causes of MissingMethodException

I got this error:


Method 'Supermarket.Domain.Entities.Product.ProductId' not found.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.MissingMethodException: Method 'Supermarket.Domain.Entities.Product.ProductId' not found.


on this code:


private Ent Load(object id)
{
    return                
        _list.SingleOrDefault(x =>
        {
            object value = typeof(Ent).InvokeMember(PrimaryKeyName,
                System.Reflection.BindingFlags.GetProperty, 
                null, x, new object[] { id });

            return value.Equals(id);
        });
}



And I'm very sure the ProductID is existing and its accessibilities are all public.

public class Product
{    
    [Key]
    public virtual int ProductId { get; set; }
    public virtual string ProductName { get; set; }
    public virtual string Category { get; set; }
}

Care to spot where the error is?

The error is I forgot to remove the id on args argument, a property getter cannot have any argument

private Ent Load(object id)
{
    return                
        _list.SingleOrDefault(x =>
        {
            object value = typeof(Ent).InvokeMember(PrimaryKeyName,
                System.Reflection.BindingFlags.GetProperty, 
                null, x, new object[] { id }); // id must be removed

            return value.Equals(id);
        });
}

No comments:

Post a Comment