.NET 4.0 / C# - 动态创建对象

use*_*142 5 .net c# asp.net visual-studio

我有 6 个 XDocument:

XDocument parametersXDoc = new XDocument(new XElement(file.Root.Element("ArrayOfParameter")));
XDocument criteriaXDoc = new XDocument(new XElement(file.Root.Element("ArrayOfCriteria")));
XDocument sortfieldsXDoc = new XDocument(new XElement(file.Root.Element("ArrayOfSortField")));
XDocument selectedfieldsXDoc = new XDocument(new XElement(file.Root.Element("ArrayOfSelectedField")));
XDocument reportlayoutXDoc = new XDocument(new XElement(file.Root.Element("ReportLayout")));
XDocument dictionaryXDoc = new XDocument(new XElement(file.Root.Element("Dictionary")));
Run Code Online (Sandbox Code Playgroud)

我想将它们全部作为参数传递给一个方法。我可以将它们作为数组传递,但是我需要知道我需要的 XDocument 的位置/索引 - 这看起来很混乱。

是否可以动态创建一个临时包装对象(带有属性),指向每个 XDocument 变量并传递它?

Dr.*_*ABT 5

您可以使用dynamic关键字或.NET4.0 中的ExpandoObject动态创建和操作动态对象。dynamic 关键字非常强大。

PetaPoco micro-ORM 中可以找到如何在数据访问层中成功使用它的开源示例。

MSDN

Visual C# 2010 引入了一种新类型,dynamic。该类型是静态类型,但动态类型的对象绕过静态类型检查。在大多数情况下,它的功能就像它具有类型对象一样。在编译时,假定类型为 dynamic 的元素支持任何操作

ExpandoObject 代码示例

using System;
using System.Dynamic;
using System.Xml.Linq;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            // Creation and population of the ExpandoObject    
            // Add as many or few properties as you like. 
            // Property Types are set at runtime and for the lifetime of the
            // property 
            // Expando objects also support dynamic methods. 
            dynamic wrapper = new ExpandoObject(); 
            wrapper.FirstProperty = "Hello";
            wrapper.SecondProperty = "Dynamic";
            wrapper.AnotherProperty = "World!";
            wrapper.AnyTypeProperty = 1234;
            wrapper.XDocumentProperty = new XDocument();
            // etc 

            // Passing of ExpandoObject
            PassWrapperToFunction(wrapper);
            Console.ReadLine();
        }

        // .. 
        // Function signature of recipient
        private static void PassWrapperToFunction(dynamic wrapper)
        {
            Console.WriteLine("{0} {1} {2} {3}\n", 
                wrapper.FirstProperty, 
                wrapper.SecondProperty,
                wrapper.AnotherProperty, 
                wrapper.AnyTypeProperty);

            Console.WriteLine("Parameter types:\n{0}\n{1}\n{2}\n{3}\n{4}",
                wrapper.FirstProperty.GetType(), 
                wrapper.SecondProperty.GetType(),
                wrapper.AnotherProperty.GetType(), 
                wrapper.AnyTypeProperty.GetType(), 
                wrapper.XDocumentProperty.GetType());
        } 
    }
}
Run Code Online (Sandbox Code Playgroud)

输出

在此处输入图片说明


Mat*_*vey 4

可以创建所谓的“匿名类型”,但这不允许您在不使用动态的情况下访问其他方法中的属性。

将所有内容都包含在一个类中有何不好?

public class Documents
{
    public XDocument ParametersXDoc { get; set; }
    public XDocument CriteriaXDoc { get; set; }
    public XDocument SortfieldsXDoc { get; set; }
    public XDocument SelectedfieldsXDoc { get; set; }
    public XDocument ReportlayoutXDoc { get; set; }
    public XDocument DictionaryXDoc { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

该类的编写时间可能比你的 stackoverflow 问题要少;)