sinat_31865329
MuzeClay
2016-09-27 02:33

C#小白求问:“扩展方法必须在非泛型静态类中定义”如何解决

using System;

namespace _02PrintMember
{
public interface IMemberExpression
{
string PrintMember(Func p);
T Obj { get; set; }
}

class MemberExpression<T> : IMemberExpression<T>
{
    private User actress;
    private Project project;
    private T _obj;


    public T Obj
    {
        get { return _obj; }
        set { _obj = value; }
    }

    T IMemberExpression<T>.Obj
    {
        get
        {
            throw new NotImplementedException();
        }

        set
        {
            throw new NotImplementedException();
        }
    }

    public MemberExpression(User actress)
    {
        this.actress = actress;
    }

    public MemberExpression(Project project)
    {
        this.project = project;
    }



    public static IMemberExpression<T> PrintMember<TP, TResult>(this IMemberExpression<T> source, Func<T, TResult> action)
    {
        TResult result = action(source.Obj);
        System.Console.WriteLine(result);
        return source;
    }

    string IMemberExpression<T>.PrintMember(Func<object, object> p)
    {
        throw new NotImplementedException();
    }
}

}
这是定义的一个接口
当我使用接口类型定义PrintMember的时候就会出现问题
问题如下:
图片说明

下面是主函数的定义
static void Main(string[] args)
{
User actress = new User() { FirstName = "Jennifer", LastName = "Lawrence", Age = 25};
Project project = new Project() { ProjectId = 1, ProjectName = "Bank System",
StartDate = DateTime.Now, DueDate = DateTime.Now.AddMonths(2) };

        //To Do : Add interface IMemberExpression and class MemberExpression
        IMemberExpression<User> userExpression = new MemberExpression<User>(actress);
        IMemberExpression<Project> projectExpression = new MemberExpression<Project>(project);

        System.Console.WriteLine("******User******");
        userExpression.PrintMember(e => e.FirstName)
            .PrintMember(e => e.LastName)
            .PrintMember(e => e.Age);

        System.Console.WriteLine("******Project******");
        projectExpression.PrintMember(e => e.ProjectId)
            .PrintMember(e => e.ProjectName)
            .PrintMember(e => e.StartDate)
            .PrintMember(e => e.DueDate);

        System.Console.ReadLine();
    }


            问题:如题
  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

2条回答

为你推荐

换一换