English 中文(简体)
如何将两个类似的职能变成一个职能?
原标题:How to make two similar functions into one function?
  • 时间:2012-05-25 21:06:21
  •  标签:
  • c#
  • .net

我有两个功能,在两个不同的班级上 做基本上相同的事情... 每个班级都有不同的属性

例如:

public class ClassA
{
    public int ColorID {get;set;}
    public string ColorDescription {get;set;}
}

public class ClassB
{
    public int TypeID {get;set;}
    public string TypeDescription {get;set;}
}

public void ExFunctionSaveA(ClassA aClass)    
{
  aClass.ColorID=1;
  aClass.ColorDescription="My Color";
  Save();
}

public void ExFunctionSaveB(ClassB bClass)    
{
  bClass.TypeID=2;
  bClass.TypeDescription="My Type";
  Save();
}

正如你可以看到的, 班级和函数有相同的类型结构, 只有财产名称是不同的... 但我觉得我重复了代码 这样做

是否有一种方法可以将ExFunctionA和ExFunctionB 合并成一个函数, 这样我就可以使用这个功能 用于所有结构相似的班级

我知道我可以做一些一般的事情 比如

public void ExFunctionSave<T>()   // T is either ClassA or ClassB
{
   .
   .
   .
   .
   Save();
}

但是,我如何处理每个

最佳回答

如果您可以改变班级的定义, 那么最好的办法是让班级执行一个包含您想要访问的属性的通用界面 :

public interface IDescribable
{
    int ID { get; set; }
    string Description { get; set; }
}

public class ClassA
{
    public int ID { get; set; }
    public string Description { get; set; }

    public int ColorID 
    { 
        get { return ID; } 
        set { ID = value; } 
    }

    public string ColorDescription 
    { 
        get { return Description; } 
        set { Description = value; } 
    }
}

public class ClassB
{
    public int ID { get; set; }
    public string Description { get; set; }

    public int TypeID 
    { 
        get { return ID; } 
        set { ID = value; } 
    }

    public string TypeDescription 
    { 
        get { return Description; } 
        set { Description = value; } 
    }
}

public void ExFunctionSave(IDescribable d, int id, string desc)    
{
    d.ID = id;
    d.Description = desc;
    Save();
}
问题回答

与其使用通用的,不如使用继承来解决这个问题?

public class theBase
{
    string ID;
    string Description;
}
public class theColor : theBase
{
}

public class theType : theBase
{
}

public void ExFunctionSaveA(theBase base)    
{
    base.ID=1;
    base.Description="My Color";
    Save();
}

除非两个类别执行具有此功能的相同接口,否则你无法做更多的工作。在您的情况下,即使函数签名也不同。

You could define an Interface with attributes id and description. The clases that has this structure could implement that interface. And your method receive as parameter the interface and execute the moethods ...

看看",http://msdn.microsoft.com/en-us/library/f7ykdhsy%28v=vs.110%29.aspx" rel=“nofollow” >Reflection

反射将允许您的代码接收 ClassA , 并发现它有 ColourID Color Description 。 同样, 当您收到 ClassB 时, 您可以发现它的 TypeID TypeDescription 。 它很酷 。

我或许会推荐一个共同的界面,至少就你而言, 但如果你再尝试更复杂、更通用的东西, 反省是前进之路。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签