简体   繁体   English

使用其他人共有的方法实现通用C#类

[英]Implementing a Generic C# class using method in common to others

I have the following problem in C# . 我在C#C#以下问题。

I have two classes exposing some common members and methods, for example: 我有两个类公开了一些常见的成员和方法,例如:

Class myClass01{
   public int a;
   public int b;
   public void method_01(){...}
   public void method_A(){...}
}

and

Class myClass02{
   public int a;
   public int b;
   public void method_01(){...}
   public void method_B(){...}
}

I want to create a new generic class that can use the common attributes/methods of both of them. 我想创建一个新的通用class ,可以使用它们两者的通用属性/方法。 For example: 例如:

Class myGeneric<T>{
   public T element;

   public int sum(){
       return element.a + element.b;
   }

   public void methodGeneric(){
       element.method_01();
    }
}

But with this kind of implementation I receive errors because elements are not accessible. 但是通过这种实现方式,我会收到错误消息,因为无法访问元素。 Is there a way to a create a generic class using methods/attributes from different classes but equally defined? 有没有一种方法可以使用来自不同类但定义相同的方法/属性来创建泛型类?

The generic class you are making currently does not have a constraint to know from what class the generic type T must be derived from. 当前正在创建的泛型class没有约束来知道泛型T必须从哪个class派生。 Thus, it assumes object which does not recognized any customized public/protected fields, properties, and methods you implement. 因此,它假定object无法识别您实现的任何自定义public/protected字段,属性和方法。

If you want the generic T to recognize the public/protected fields, properties, and methods you have in common between two classes, you should create an interface which is implemented by both classes and you have the generic type T derived from that particular interface . 如果希望通用T识别两个类之间共有的public/protected字段,属性和方法,则应创建一个由两个类都实现的interface ,并且具有从该特定interface派生的通用T

For example: 例如:

public interface ImyClass { //this is the common interface
    int a { get; set; }
    int b { get; set; }
    void method_01();
}

class myClass01 : ImyClass { //your special interface implementation for myClass01
    public int a { get; set; }
    public int b { get; set; }
    public void method_01() { }
    public void method_A() { }
}

class myClass02 : ImyClass {  //your special interface implementation for myClass02
    public int a { get; set; }
    public int b { get; set; }
    public void method_01() { }
    public void method_B() { }
}

Then in your generic, constraint the generic type with the interface 然后在您的泛型中,使用interface约束泛型类型

class myGeneric<T> where T : ImyClass {
    public T element;

    public int sum() {              
        return element.a + element.b;
    }

    public void methodGeneric() {
        element.method_01();
    }
}

You could also use abstract or simple base class if the implementation for both methods (both method_01 ) happen to be exactly the same, for example: 如果两个方法(两个method_01 )的实现都完全相同,则也可以使用abstract或简单基class ,例如:

public abstract class myAbstractClass { //this is the common abstract class or base class
    public int a;
    public int b;
    public void method_01(){ } // all your common implementations
}

public class myClass01 : myAbstractClass {
    public void method_A() { } //your special implementation for myClass01
}

public class myClass02 : myAbstractClass {
    public void method_B() { } //your special implementation for myClass02
}

class myGeneric<T> where T : myAbstractClass { //generic specifically from myAbstractClass 
    public T element;

    public int sum() {              
        return element.a + element.b;
    }

    public void methodGeneric() {
        element.method_01();
    }
}

In any case, a common inheritance is required to recognize the public/protected fields, properties, and methods. 在任何情况下,都需要公共继承来识别public/protected字段,属性和方法。

Use Interfaces and "where" to get the members accessible. 使用Interfaces和“ where”可访问成员。 Eg: 例如:

public class SortedList<T> : GenericList<T> where T : System.IComparable<T>

https://msdn.microsoft.com/en-us/library/kwtft8ak.aspx https://msdn.microsoft.com/zh-CN/library/kwtft8ak.aspx

Note that in addition to Ians answer when using an interface, the generic is not necessary anymore you could simple use 请注意,除了使用接口时的Ians回答外,不再需要泛型了,您可以简单地使用它

class myGeneric {
    public ImyClass element;

    public int sum() {              
        return element.a + element.b;
    }

    public void methodGeneric() {
        element.method_01();
    }
}

Although it was not the actual question 虽然这不是实际的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM