简体   繁体   English

是否可以为XAttribute AND XElement编写方法?

[英]Is it possible to write a method for an XAttribute AND XElement?

XAttribute and XElement both derive from the XObject type. XAttributeXElement都派生自XObject类型。

Both have a Value property. 两者都有Value属性。

So far, this is what I've come up with to do what I want : 到目前为止,这就是我想要做的事情:

string FooMyXObject( XObject bar ){
    if ( bar.NodeType == NodeType.Element )
        return ( bar as XElement ).Value;
    else if ( bar.NodeType == NodeType.XAttribute )
        return ( bar as XAttribute ).Value;
    else
        throw new Exception( "Generic Fail Message" );
}

This feels clunky. 这感觉很笨重。 I'm hoping there is some way to make this less clunky. 我希望有一些方法可以让它变得不那么笨重。 Some way of accessing the Value property from the XObject , since they both have a string property names Value . XObject访问Value属性的一些方法,因为它们都有一个字符串属性名称Value

Is this possible, or am I just forced to doing it in thus clunky fashion? 这是可能的,还是我只是被迫以如此笨重的方式做到这一点?

If the .Value property is defined on the XObject, Then you can write this pretty simply using generics. 如果在XObject上定义了.Value属性,那么你可以使用泛型简单地编写它。

ie

string FooMyXObject<T>(T bar) where T : XObject
{
    return bar.Value;
}

If that is not the case then you should consider moving it to the base class if it is common functionality of the derived classes. 如果不是这种情况,那么如果它是派生类的通用功能,则应考虑将其移动到基类。

Edit: Your code is pretty much right if it is the case the Value property is not on the base class. 编辑:如果Value属性不在基类上,则代码非常正确。 Maybe you could refactor it like so using a case for easier extension: 也许你可以使用一个案例来重构它,以便更容易扩展:

string FooMyXObject(XObject bar)
{
   switch(bar.NodeType)
   {
      case NodeType.Element:
          return ( bar as XElement ).Value;
      case NodeType.XAttribute:
          return ( bar as XAttribute ).Value;
      default:
          throw new Exception( "Generic Fail Message" );
   }
}

or just for fun 或只是为了好玩

string FooMyXObject(XObject bar)
    {
       try
       {
           dynamic temp = bar;
           return temp.Value;
       } 
       catch ()
       {
           throw new Exception( "Generic Fail Message" );
       }
    }

You can cast bar to dynamic to avoid separate branches: 您可以将bardynamic以避免单独的分支:

string FooMyXObject( XObject bar ){
    if (bar.NodeType == NodeType.Element || bar.NodeType == NodeType.XAttribute)
        return ((dynamic)bar).Value;
    else
        throw new Exception( "Generic Fail Message" );
}

The answers given by @meganaut and @dasblinkenlight are both correct, but there is more to be said. @meganaut和@dasblinkenlight给出的答案都是正确的,但还有更多要说的。

In the System.Linq.Xml namespace, XElement and XAttribute both extend XObject , but they each define their Value property separately, so this kind of branching or the use of the dynamic type is required. System.Linq.Xml命名空间中, XElementXAttribute都扩展了XObject ,但它们分别定义了它们的Value属性,因此需要这种分支或使用dynamic类型。

In other object models, generics might be usable in a similar scenario as suggested by @meganaut. 在其他对象模型中,泛型可能在@meganaut建议的类似场景中可用。

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

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