简体   繁体   English

C#使用基类引用调用派生类属性

[英]C# call derived class property using base class reference

I have a class RequestDetail . 我有一个类RequestDetail There are 2 classes classA and classB derived from it such that each of them has their own property: 有两个类classA和classB派生自它们,因此它们每个都有自己的属性:

public partial classA : RequestDetail { ..... }
public partial classB : RequestDetail { ..... }

I am writing a method CreateMethod1(ClassA a) and CreateMethod2(ClassB b) . 我正在写一个方法CreateMethod1(ClassA a)CreateMethod2(ClassB b)

Both methods doing the same stuff except some minor difference. 两种方法都做同样的事情,除了一些细微的差别。 I would like to write a generic method and call that method by passing the reference in CreateMethod1 and CreateMethod2. 我想编写一个通用方法,并通过在CreateMethod1和CreateMethod2中传递引用来调用该方法。

Can anyone help me in doing this? 有人可以帮我吗?

THanks 谢谢

EDIT: 编辑:

What I have excluded is I have received a WSDL which when generated gives me four separate classes that inherit from a base class with around 20 properties. 我所排除的是,我收到了一个WSDL,当它生成时会给我四个独立的类,这些类从具有大约20个属性的基类继承。 They only differ very slightly, to be exact 2 classes contain the same field (IsUrgent), the third contains (Ticket and Reason) and the fourth contains (BudgetCode) The persistance however is exactly the same for all implementations. 它们仅稍有不同,确切地说,2个类包含相同的字段(IsUrgent),第三个包含(Ticket和Reason),第四个包含(BudgetCode)。持久性对于所有实现都是完全相同的。 I dont want to create 4 seperate methods to persist the same information. 我不想创建4个单独的方法来保留相同的信息。

Its worth noting the classes are partial. 值得注意的是这些类是局部的。

xsd looks like following XSD看起来像以下

<xs:complexType name="ClassA">
    <xs:complexContent>
      <xs:extension base="IARequestDetails">
        <xs:sequence>
          <xs:element name="IsUrgent" type="IAUrgency"/>
        </xs:sequence>
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>

  <xs:complexType name="ClassB">
    <xs:complexContent>
      <xs:extension base="IARequestDetails">
        <xs:sequence>
          <xs:element name="BudgetCode" type="ProjectBudgetCode"/>
          <xs:element name="IsUrgent" type="IAUrgency"/>
        </xs:sequence>
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>

您应该在基类中创建一个virtualabstract方法,在每个派生类中重写它,然后正常调用它。

Specify your base class as abstract, write the method "CreateMethod" inside it and create abstract functions where different behavior is required (or virtual if the behavior overriding is optional). 将您的基类指定为抽象类,在其中编写方法“ CreateMethod”,并在需要不同行为的情况下创建抽象函数(如果行为替代是可选的,则创建虚拟函数)。 Override the abstract functions, adding the desired behaviors inside them. 覆盖抽象函数,在其中添加所需的行为。

public abstract RequestDetail
{
    public void CreateDetail()
    {
        CustomBehavior();
    }
    protected abstract CustomBehavior();
}
public RequestDetailA : RequestDetail
{
    protected override void CustomBehavior()
    {
        // Foo
    }
}

if those method are in RequestDetails i suggest you make it receive a RequestDetail instead and in that method you can call an abstract method from RequestDetail that does those minor differences and each of ClassA and ClassB implement the different approach something like 如果这些方法在RequestDetails中,我建议您改为让它接收一个RequestDetail,并且在该方法中,您可以从RequestDetail调用一个抽象方法,该方法具有一些细微差别,并且ClassA和ClassB均实现类似的方法

class RequestDetail{
    public void CreateMethod(RequestDetails reqD){ 
        //do what you need
        AbstCreateMethod(reqD);
    }
    public abstract void AbstCreateMethod(RequestDetails reqD);

}

class ClassA : RequestDetails{
    public void AbstCreateMethod(RequestDetails reqD){
        //do classA things  
    }
}

class ClassB : RequestDetails{
    public void AbstCreateMethod(RequestDetails reqD){
        //do classB things  
    }
}

if it's in another class follow a similar solution to this but with the class that has those methods. 如果在另一个类中,请遵循类似的解决方案,但要使用具有这些方法的类。 For more information on this read the design pattern Template method 有关更多信息,请阅读设计模式Template方法

class mybase 
{ 
   virtual void DoStuff(){...}

}

class Der1:mybase
{
    virtual override void DoStuff(){
       base.DoStuff(); // Call the Common Code 
       .... Custom Code for this class
 }

 Der1 A = new Der1(); 
 mybase B = new Der1();
 A.DoStuff(); // this will call the derived version 
 B.DoStuff(); // So will this 

Here is a solution using partial classes an interface and generic constraints ... you will need to build the list of parameters that need to be returned in the Params implemenation for each of your sub classes, super class properties are naturally available. 这是一个使用部分类,接口和通用约束的解决方案...您将需要为每个子类建立在Params实现中需要返回的参数列表,超类属性自然可用。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

namespace ConsoleApplication3
{
    public interface IABInterface
    {
        //Could be your SqlParameters
        string[] Params { get; }
    }

    public partial class IARequestDetails
    {
        public int One
        {
            get; set; 
        }

        public int Two
        {
            get;set;
        }
    }

    public partial class ClassA : IARequestDetails
    {
        public int IsUrgent 
        {
            get;set;
        }
    }

    public partial class ClassB : IARequestDetails
    {
        public int BudgetCode
        {
            get;
            set;
        }

        public int IsUrgent
        {
            get;
            set;
        }
    }

    public partial class ClassA : IABInterface
    {
        #region IABInterface Members

        public string[] Params
        {
            //Create your list of parameters
            get { return null; }
        }

        #endregion
    }

    public partial class ClassB : IABInterface
    {
        #region IABInterface Members

        public string[] Params
        {
            get
            {
                return null;
            }
        }
        #endregion
    }

    public class Persist
    {
        public void Save<T>(T obj)
            where T : IARequestDetails, IABInterface
        {
            //Do you saving here ... 
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Persist persist = new Persist();

            persist.Save(new ClassA());
            persist.Save(new ClassB());
        }
    }
}

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

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