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

时间:2012-01-26 09:30:55

标签: c# asp.net .net visual-studio

我有6个XDocuments:

        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")));

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

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

6 个答案:

答案 0 :(得分:3)

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

将整个课程包装在课堂上有什么不好?

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; }
}

该类可能比stackoverflow问题花费更少的时间来写;)

答案 1 :(得分:1)

我应该使用字典而不是数组,在这种情况下,将使用键来识别它们中的每一个。

var parameters = new Dictionary<String, XDocument>();
parameters["parametersXDoc"] = new XDocument(new XElement(file.Root.Element("ArrayOfParameter")));
parameters["criteriaXDoc"] = new XDocument(new XElement(file.Root.Element("ArrayOfCriteria")));
parameters["sortfieldsXDoc"] = new XDocument(new XElement(file.Root.Element("ArrayOfSortField")));
parameters["selectedfieldsXDoc"] = new XDocument(new XElement(file.Root.Element("ArrayOfSelectedField")));
parameters["reportlayoutXDoc"] = new XDocument(new XElement(file.Root.Element("ReportLayout")));
parameters["dictionaryXDoc"] = new XDocument(new XElement(file.Root.Element("Dictionary")));

答案 2 :(得分:1)

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

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

来自MSDN

  

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

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());
        } 
    }
}

<强>输出

enter image description here

答案 3 :(得分:0)

如果您不想传递强类型对象,那么您的快速和脏选项是:

  1. 传递一个匿名对象:
  2.   

    new { Doc1=parametersXDoc , Doc2=criteriaXDoc , ... .... ..}

    1. 传递元组:
    2.   

      new Tuple<XDocument,XDocument,XDocument,XDocument,XDocument,XDocument>

      1. 通过Dictionary<String,XDocument>

答案 4 :(得分:0)

您可以考虑创建一个包含属性的简单类型。将匿名类型传递给方法看起来有点不好。如果你还想看一下,你可以试试这个:

http://msdn.microsoft.com/en-us/library/bb397696.aspx

但是,正如我所说:

  

传递匿名类型或包含匿名的集合   types,作为方法的参数,可以将参数声明为   类型对象。但是,这样做会破坏强类型的目的。   如果必须存储查询结果或将其传递到方法之外   边界,考虑使用普通的命名结构或类而不是   匿名类型。

答案 5 :(得分:0)

为什么不使用字典 - 然后您可以使用简单的字符串键,也可以使用文档中的唯一属性作为键。例如:

public void FooMethod(Dictionary<String, XDocument> docDictionary)
{
    var doc1 = docDictionary["parametersXDoc"];
    var doc2 = docDictionary["criteriaXDoc"];
    blah blah blah
}

干杯, 克里斯。