简体   繁体   English

这是处理泛型的唯一属性的好方法吗?

[英]Is this a good way to handle unique properties for a generic?

I have a generic class that represents a document that can be of currently only two types. 我有一个泛型类,代表一个目前只有两种类型的文档。 Type1 or Type2. Type1或Type2。 Most of the methods and properties work perfectly for both types but the title is different. 大多数方法和属性都适用于两种类型,但标题不同。 What I am wondering is if there is a better way to handle this? 我想知道的是,是否有更好的方法来处理这个问题? Thanks!! 谢谢!!

[XmlIgnore]
public string DocumentType
{
  get
  {
    return typeof(T).Name;
  }
}

[XmlIgnore]
public string DocumentTitle
{
  get
  {
    string retval = string.Empty;
    Object obj = Document;

    switch (DocumentType)
    {
      case "Type1":
        retval = ((Type1)obj).title.Text;
        break;
      case "Type2":
        retval = ((Type2)obj).Title;
        break;
    }
    return retval;
  }
}

Type1 and Type2 were generated using xsd.exe so I hesitate to change them although maybe adding a readonly xml ignored property to get the title in both Type1 and Type2 that is consistent? Type1和Type2是使用xsd.exe生成的,所以我可能会更改它们虽然可能会添加一个readonly xml ignored属性来获取Type1和Type2中的标题是否一致?

Use a common interface and implement it in each class. 使用通用接口并在每个类中实现它。 If you don't want to change the original class you could try adding a wrapper around each class that implements this interface. 如果您不想更改原始类,可以尝试在实现此接口的每个类周围添加包装器。

interface IHasTitle
{
    string Title { get; }
}

class MyType1 : Type1, IHasTitle
{
    // Add constructors here.

    public string Title { get { return this.title.Text; } }
}

class MyType2 : Type2, IHasTitle
{
    // Add constructors here.
}

Then you can do this: 然后你可以这样做:

[XmlIgnore]
public string DocumentTitle
{
    get
    {
        IHasTitle hasTitle = Document;
        return hasTitle.Title;
    }
}

You might want to extend the interface to IDocument and include all the other common members such as Name , etc. 您可能希望将接口扩展到IDocument并包含所有其他常见成员,例如Name等。

Does xsd.exe generate partial classes? xsd.exe是否生成了部分类? If so, Mike's answer is cleaner than this one. 如果是这样,迈克的答案比这个更清晰。 (Create a new partial class code file for each of your type classes and implement the interface in the non-generated files.) (为每个类型类创建一个新的部分类代码文件,并在非生成的文件中实现该接口。)

Otherwise, so as not to be modifying generated code, I'd recommend you use safe-casting to determine the document type: 否则,为了不修改生成的代码,我建议您使用安全转换来确定文档类型:

    public string ExtractDocumentTitle()
    {
        Type1 t1 = Document as Type1;
        if (t1 != null)
            return t1.title.Text;

        Type2 t2 = Document as Type2;
        if (t2 != null)
            return t2.Title;


        // fall-through & catch-all
        return String.Empty;
    }

XSD's can also handle inheritance. XSD还可以处理继承。 Define the base complexType (possibly with abstract="true") then create 2 complexType's which extend the base one. 定义基本complexType(可能使用abstract =“true”),然后创建2个complexType,扩展基类。 If you generate code from it, it will also reflect this inheritance. 如果从中生成代码,它也会反映此继承。

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

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