Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 什么';这是处理层次结构中具有相同属性的不同类的最有效方法_C#_.net_Xml_Binding_Xsd - Fatal编程技术网

C# 什么';这是处理层次结构中具有相同属性的不同类的最有效方法

C# 什么';这是处理层次结构中具有相同属性的不同类的最有效方法,c#,.net,xml,binding,xsd,C#,.net,Xml,Binding,Xsd,我已经搜索过了,看看是否已经存在关于这个的问题,所以如果我错过了,我道歉 我在代码中嵌入了一个复杂的类结构,它是通过在xml模式上运行xsd.exe生成的绑定生成的,因此我没有创建该结构,但我必须处理它 层次结构中的类中有一些重复的属性。出于保密原因,我无法发布实际代码,但例如,如果我有一个基类,其中有10个类继承自它,其中8个类中有一个名为WidgetValue的属性,我如何才能最有效地处理这个问题 目前,我认为我必须检查每个继承类的类型,然后强制转换到每个继承类以访问WidgetValue属

我已经搜索过了,看看是否已经存在关于这个的问题,所以如果我错过了,我道歉

我在代码中嵌入了一个复杂的类结构,它是通过在xml模式上运行xsd.exe生成的绑定生成的,因此我没有创建该结构,但我必须处理它

层次结构中的类中有一些重复的属性。出于保密原因,我无法发布实际代码,但例如,如果我有一个基类,其中有10个类继承自它,其中8个类中有一个名为WidgetValue的属性,我如何才能最有效地处理这个问题

目前,我认为我必须检查每个继承类的类型,然后强制转换到每个继承类以访问WidgetValue属性


但是,有没有更好的方法可以解决这个问题?

正如前面所指出的,您不应该修改生成的类,因为如果出于任何原因需要重新生成它们,您还需要再次修改它们

因此,我想:

  • 使用
    动态
    ,或
  • 使用
    Reflection
但首先,这是我用于测试的模型:

public class MainClass
{
    public string MainProp { get; set; }
}

public class ClassA : MainClass
{
    public string SomeProp { get; set; }

    public string WidgetValue { get; set; }
}

public class ClassB : MainClass
{
    public string SomeOtherProp { get; set; }

    public string WidgetValue { get; set; }
}

public class ClassC : MainClass
{
    public string AnotherProp { get; set; }

    public string WidgetValue { get; set; }
}

public class ClassD : MainClass
{
    public string Different { get; set; }
}
这就是使用
动态
反射
获取WidgetValue的方法:

static void GetWidgetValue(MainClass obj)
    {
        Type[] typesThatHaveWidgetValue = new Type[] { typeof(ClassA), typeof(ClassB), typeof(ClassC) };

        if (typesThatHaveWidgetValue.Contains(obj.GetType()))
        {
            dynamic o = obj;
            Console.WriteLine(o.WidgetValue);
        }
        else
        {
            Console.WriteLine("Object don't have property 'WidgetValue'");
        }
    }

    static void GetWidgetValueWithReflection(MainClass obj)
    {
        var type = obj.GetType();
        var widgetValueProp = type.GetProperty("WidgetValue");
        if (widgetValueProp != null)
        {
            var widgetValue = widgetValueProp.GetValue(obj);
            Console.WriteLine(widgetValue);
        }
        else
        {
            Console.WriteLine("Object don't have property 'WidgetValue'");
        }
    }
这就是你测试它的方式:

var classA = new ClassA { WidgetValue = "The Value" };
        var classB = new ClassB { WidgetValue = "The Other Value" };
        var classD = new ClassD();

        GetWidgetValue(classA); // The Value
        GetWidgetValue(classB); // The Other Value
        GetWidgetValue(classD); // Object don't have property 'WidgetValue'

        GetWidgetValueWithReflection(classA); // The Value
        GetWidgetValueWithReflection(classB); // The Other Value
        GetWidgetValueWithReflection(classD); // Object don't have property 'WidgetValue'
然后你避免做类似以下的事情:

if (obj.GetType() == typeof(ClassA))
{
    var classA = (ClassA)obj;
    Console.WriteLine(classA.WidgetValue);
}
else if (obj.GetType() == typeof(ClassB))
{
    var classB = (ClassB)obj;
    Console.WriteLine(classB.WidgetValue);
} ...

保密性并不妨碍您提供一个简单但具体的示例。此外,“高效”还需要细化。时间效率?空间效率?开发时间效率?吝啬的表达?在没有进一步信息的情况下,我们所能做的就是抽象地提出备选方案,如:(1)接受重复;(2) 将
WidgetValue
提升到基类,但使其成为可选的;(3) 为确实需要
WidgetValue
的8个类创建一个中间类。对于你的目的来说,以下哪一项更“有效”还不清楚。抱歉没有说得更清楚。我的意思是在开发时间效率方面。在较小程度上,代码会变得多么复杂。因为课堂上有很多地方会发生这种事情,所以无论我最后做什么,我都要做很多。如果执行2或3,则必须修改类,这意味着每次重新发布XSD时都要这样做。这里可能还有一个问题,我最终需要输出符合模式的XML,我不想破坏与模式的兼容性。除非需要,否则不要手动修改它。否则,在下次出于任何原因需要重新生成类时,它将消失。反射方法工作得非常好。谢谢你的指导。我已经投了赞成票,但我现在没有足够的分数公开计算!