需要有关泛型,接口和基类的帮助

时间:2012-04-24 16:17:14

标签: c# generics interface ado.net

假设我有这两个对象:

public class Object1
{
  string prop1;
  string prop2;
}

public class Object2
{
  string prop1;
  int prop2;
  int prop3;
}

以及以下课程&方法:

public class Object1Service
{
  public Object1 GetObject(Object1 o) { return o; }
  public void SaveProperty2(Object1 o, string s) { o.prop2 = s; }
}

public class Object2Service
{
  public Object2 GetObject(Object2 o) { return o; }
  public void SaveProperty2(Object2 o, int i) { o.prop2 = i; }
}

如何将其转换为通用? 我最好需要一些东西,以便服务实现一个接口,并在可能的情况下调用一些通用的基类。
如果这两个对象共享一个共同的父类会有帮助吗?如果是这样,它们将如何构建?


附录:

回归是我真正的问题。

public T GetObjectByKey<T>(string key)
{
    using (DBEntities db = new DBEntities())
    {
        try
        {
            T returnedEntity = default(T);

            switch (EntityDictionary[typeof (T)])
            // I have a dictionary setup like this dictionary<type, string>
            {
                case "Object1" :
                    returnedEntity = ( from r in db.ObjectONESets
                                        where r.prop1 == key
                                        select new T
                                        {
                                            prop1 = r.prop1,
                                            prop2 = r.prop2
                                        }).FirstOrDefault();
                    break ;
                case "Object2" :
                    returnedEntity = ( from r in db.ObjectTWOSets
                                        where r.prop1 == key
                                        select new T
                                        {
                                            prop1 = r.prop1,
                                            prop2 = r.prop2,
                                            prop3 = r.prop3
                                        }).FirstOrDefault();
                    break ;
            }
            return returnedEntity;
        }
        catch (NullReferenceException )
        {
            return default(T);
        }
    }
}

不将所有对象属性放在基础对象中,它无法知道prop1-3属于T的属性。
如果我确实将所有属性(普通或非常见)放在基础对象中,并且如果我需要Object1,则它会附加一个prop3,我不需要它。
我不确定我是否在这一点上有多大意义,或者我所要求的甚至可以用泛型。

3 个答案:

答案 0 :(得分:5)

我相信你不会在这里使用泛型获益。原因是 prop2在每个类中都是不同的类型。如果它们是相同的,那么你可以将公共属性放入基类并执行类似这样的操作

public class BaseObject
{
    string prop1;
    string prop2
}

public class Object1 : BaseObject
{

}

public class Object2 : BaseObject
{
    int prop3;
}

public class ObjectService<T> where T is BaseObject
{
    public T GetObject(T o) { return o; }
    public void SaveProperty2(T o, string i) { o.prop2 = i; }
}

你至少可以使用你当前的例子使GetObject成为通用的......但是整个方法看起来真的没有意义:

public class ObjectService
{
  public T GetObject<T>(T o) { return o; }
  public void SaveObject2Property2(Object2 o, int i) { o.prop2 = i; }
  public void SaveObject1Property2(Object1 o, string s) { o.prop2 = s; }
}

答案 1 :(得分:3)

这是一个解决方案,应该做你想要的事情:

public interface IPropertyProvider<T>
{
    T Prop2 { get; set; }
}

public class ObjectService<T, TProp> where T : IPropertyProvider<TProp>
{
    public void SaveProperty(T o, TProp i)
    {
        o.Prop2 = i;
    }
}

public class Object1 : IPropertyProvider<string>
{
    public string Prop1 { get; set; }
    public string Prop2 { get; set; }
}

public class Object2 : IPropertyProvider<int>
{
    public string Prop1 { get; set; }
    public int Prop2 { get; set; }
    public int Prop3 { get; set; }
}

public class Object1Service : ObjectService<Object1, string>
{
}

public class Object2Service : ObjectService<Object2, int>
{
}

答案 2 :(得分:1)

Justin是正确的,没有修改OP的对象,就没有办法使这个“整齐”通用。

这是一个混乱的解决方案:

interface IObject<T>
{
    T prop2 {get;set;}
}

class ObjectService<T, Z> where T : IObject<Z>
{
    public T GetObject(T o)
    {
        return o;
    }

    public void SetValue(T o, Z val)
    {
        o.prop2 = val;
    }
}