C# 将多个对象类合并到泛型类中

C# 将多个对象类合并到泛型类中,c#,class,generics,C#,Class,Generics,我正在维护一个现有的C#应用程序,该应用程序将三个对象实现为除名称外完全相同的类 public class Role1 { public string lala {get;set;} public string abab {get;set;} public string cxcx {get;set;} public Role1(string lala, string abab, string cxcx) { } public

我正在维护一个现有的C#应用程序,该应用程序将三个对象实现为除名称外完全相同的类

public class Role1
{
     public string lala {get;set;}
     public string abab {get;set;}
     public string cxcx {get;set;}
     public Role1(string lala, string abab, string cxcx)
     {
     }
     public List<Role1> GetMembers()
     {
         return dataaccesslayer.GetRole1Members();
     }
}

public class Role2
{
     public string lala {get;set;}
     public string abab {get;set;}
     public string cxcx {get;set;}
     public Role2(string lala, string abab, string cxcx)
     {
     }
     public List<Role2> GetMembers()
     {
         return dataaccesslayer.GetRole2Members();
     }
}

public class Role3
{
     public string lala {get;set;}
     public string abab {get;set;}
     public string cxcx {get;set;}
     public Role3(string lala, string abab, string cxcx)
     {
     }
     public List<Role3> GetMembers()
     {
         return dataaccesslayer.GetRole3Members();
     }
}
公共类角色1
{
公共字符串lala{get;set;}
公共字符串abab{get;set;}
公共字符串cxcx{get;set;}
公用角色1(字符串lala、字符串abab、字符串cxcx)
{
}
公共列表GetMembers()
{
返回dataaccesslayer.GetRole1Members();
}
}
公共类角色2
{
公共字符串lala{get;set;}
公共字符串abab{get;set;}
公共字符串cxcx{get;set;}
公共角色2(字符串lala、字符串abab、字符串cxcx)
{
}
公共列表GetMembers()
{
返回dataaccesslayer.GetRole2Members();
}
}
公开课角色3
{
公共字符串lala{get;set;}
公共字符串abab{get;set;}
公共字符串cxcx{get;set;}
公共角色3(字符串lala、字符串abab、字符串cxcx)
{
}
公共列表GetMembers()
{
返回dataaccesslayer.GetRole3Members();
}
}
在数据访问层中,成员从SharePoint列表中提取,该列表由角色名称的字符串值过滤。示例“角色1”

我尝试创建一个通用类实现,如下所示:

 public class Role
 {
     public string lala {get;set;}
     public string abab {get;set;}
     public string cxcx {get;set;}
     public RoleType RoleType {get;set;}

     public Role(RoleType roletype, string lala, string abab, string cxcx)
     {
     }
     public static List<T> GetMembers<T>(ref T roletype,... )
     {
         return dataaccesslayer.GetRoleMembers<T>(roletype,...);
     }

 }
public class Role
{
    public string lala { get; set; }
    public string abab { get; set; }
    public string cxcx { get; set; }
}

// Very likely this could just be an interface. There's not much use for
// an abstract class that doesn't implement anything. It effectively *is* 
// an interface.
public abstract class RoleMembersProvider<TRole> where TRole : Role
{
    public abstract List<TRole> GetMembers();
}

public class Role1MemberProvider : RoleMemberProvider<Role1>
{
    public override List<Role1> GetMembers()
    {
        return dataaccesslayer.GetRole1Members();
    }
}
公共类角色
{
公共字符串lala{get;set;}
公共字符串abab{get;set;}
公共字符串cxcx{get;set;}
公共角色类型角色类型{get;set;}
公共角色(RoleType RoleType、stringlala、stringabab、stringcxcx)
{
}
公共静态列表GetMembers(ref T roletype,…)
{
返回dataaccesslayer.GetRoleMembers(roletype,…);
}
}
在数据层类中:

public List<T> GetMembers<T>(ref T roletype,...)
{
    ....
    return....
}
public List GetMembers(ref T roletype,…)
{
....
返回。。。。
}
但是编译器抛出的GetMembers有一些无效参数

我错过了什么


谢谢。

如果将类分为两部分,则通用实现更有意义—角色定义和提供角色成员的内容。也许是这样的:

 public class Role
 {
     public string lala {get;set;}
     public string abab {get;set;}
     public string cxcx {get;set;}
     public RoleType RoleType {get;set;}

     public Role(RoleType roletype, string lala, string abab, string cxcx)
     {
     }
     public static List<T> GetMembers<T>(ref T roletype,... )
     {
         return dataaccesslayer.GetRoleMembers<T>(roletype,...);
     }

 }
public class Role
{
    public string lala { get; set; }
    public string abab { get; set; }
    public string cxcx { get; set; }
}

// Very likely this could just be an interface. There's not much use for
// an abstract class that doesn't implement anything. It effectively *is* 
// an interface.
public abstract class RoleMembersProvider<TRole> where TRole : Role
{
    public abstract List<TRole> GetMembers();
}

public class Role1MemberProvider : RoleMemberProvider<Role1>
{
    public override List<Role1> GetMembers()
    {
        return dataaccesslayer.GetRole1Members();
    }
}

如果将类分为两个部分(角色定义和提供角色成员的内容),则泛型实现更有意义。也许是这样的:

 public class Role
 {
     public string lala {get;set;}
     public string abab {get;set;}
     public string cxcx {get;set;}
     public RoleType RoleType {get;set;}

     public Role(RoleType roletype, string lala, string abab, string cxcx)
     {
     }
     public static List<T> GetMembers<T>(ref T roletype,... )
     {
         return dataaccesslayer.GetRoleMembers<T>(roletype,...);
     }

 }
public class Role
{
    public string lala { get; set; }
    public string abab { get; set; }
    public string cxcx { get; set; }
}

// Very likely this could just be an interface. There's not much use for
// an abstract class that doesn't implement anything. It effectively *is* 
// an interface.
public abstract class RoleMembersProvider<TRole> where TRole : Role
{
    public abstract List<TRole> GetMembers();
}

public class Role1MemberProvider : RoleMemberProvider<Role1>
{
    public override List<Role1> GetMembers()
    {
        return dataaccesslayer.GetRole1Members();
    }
}

如果它们是相同的,那么为什么不是一个抽象类呢

public abstract class Role
{
    public string lala { get; set; }
    public string abab { get; set; }
    public string cxcx { get; set; }
    public Role(string lala, string abab, string cxcx)
    {
    }
    public abstract List<Role> GetMembers();
}
公共抽象类角色
{
公共字符串lala{get;set;}
公共字符串abab{get;set;}
公共字符串cxcx{get;set;}
公共角色(字符串lala、字符串abab、字符串cxcx)
{
}
公共摘要列表GetMembers();
}

如果它们是相同的,那么为什么不是一个抽象类呢

public abstract class Role
{
    public string lala { get; set; }
    public string abab { get; set; }
    public string cxcx { get; set; }
    public Role(string lala, string abab, string cxcx)
    {
    }
    public abstract List<Role> GetMembers();
}
公共抽象类角色
{
公共字符串lala{get;set;}
公共字符串abab{get;set;}
公共字符串cxcx{get;set;}
公共角色(字符串lala、字符串abab、字符串cxcx)
{
}
公共摘要列表GetMembers();
}

为什么引用的参数是
ref
以及完整调用和实现是什么样子的?角色没有一个t引用来自我在MS网站上找到的示例。我试过了,也试过了@狗仔队T只是通话中角色类型识别类型的占位符。至少我从MS文档中是这样理解的。为什么
ref
的参数是什么?完整的调用和实现是什么样子的?角色没有一个t ref来自我在MS网站上找到的示例。我试过了,也试过了@狗仔队T只是通话中角色类型识别类型的占位符。至少从MS文档中我是这样理解的。是的。除类型外,所有类都相同。我没有编写原始代码,因此我试图简化,原因有两个,最重要的是,添加一个额外的角色目前需要复制一个现有的类,并将类型更改为新的角色,如果只有一个类,这将得到缓解。我认为我需要将它们保留为类型(对象),因为有太多的地方将它们作为类型引用。Scott,如果我理解您的建议,那么无论在哪种情况下(可能是在任何情况下),添加新角色都需要进行代码更改,即使代码更改很小,例如在新角色的select块中添加新的案例。你觉得这样对吗?Scott,还有一件事:你提到使用类型作为数据。我不确定这是不是真的发生了。此场景适用于工作流应用程序,各种角色仅决定谁在审批流程中执行下一步。比如说,如果乔·史密斯是Role1的成员,而WF要求Role1成员批准,那么乔·史密斯将被包括在内。这有意义吗?嗨,我不认为“类型为数据”发生在您所展示的代码中。如果你有三个属性完全相同的类,这是一种可能。如果您想知道它是什么类型的角色(1、2、3),您必须检查类型。另一种方法是只使用一个具有这些属性的类,然后添加另一个属性来区分它是什么类型的角色。是的。除类型外,所有类都相同。我没有编写原始代码,因此我试图简化,原因有两个,最重要的是,添加一个额外的角色目前需要复制一个现有的类,并将类型更改为新的角色,如果只有一个类,这将得到缓解。我认为我需要将它们保留为类型(对象),因为有太多的地方将它们作为类型引用。Scott,如果我理解您的建议,那么无论在哪种情况下(可能是在任何情况下),添加新角色都需要进行代码更改,即使代码更改很小,例如在新角色的select块中添加新的案例。你觉得这样对吗?Scott,还有一件事:你提到使用类型作为数据。我不确定这是不是真的