.NET 4.0/C#-动态创建对象
我有6份文件:.NET 4.0/C#-动态创建对象,c#,asp.net,.net,visual-studio,C#,Asp.net,.net,Visual Studio,我有6份文件: XDocument parametersXDoc = new XDocument(new XElement(file.Root.Element("ArrayOfParameter"))); XDocument criteriaXDoc = new XDocument(new XElement(file.Root.Element("ArrayOfCriteria"))); XDocument sortfieldsXDoc = new XDoc
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变量的临时包装器对象(带有属性)并传递它?如果您不想传递强类型对象,那么您的快速脏选项是:
new{Doc1=parametersXDoc,Doc2=criteriaXDoc,…}
新元组
字典
可以创建一个所谓的“匿名类型”,但如果不使用动力学,就无法访问另一个方法中的属性 把所有的东西都放在一节课上有什么不好
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问题花费更少的时间编写;) 我应该使用字典而不是数组,在这种情况下,我将使用键来识别它们中的每一个
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")));
var参数=新字典();
参数[“parametersXDoc”]=新的XDocument(新的XElement(file.Root.Element(“ArrayOfParameter”));
参数[“criteriaXDoc”]=新的XDocument(新的XElement(file.Root.Element(“ArrayOfCriteria”));
参数[“sortfieldsXDoc”]=新的XDocument(新的XElement(file.Root.Element(“ArrayOfSortField”));
参数[“selectedfieldsXDoc”]=新的XDocument(新的XElement(file.Root.Element(“ArrayOfSelectedField”));
参数[“reportlayoutXDoc”]=新的XDocument(新的XElement(file.Root.Element(“ReportLayout”));
参数[“dictionaryXDoc”]=newxdocument(new-XElement(file.Root.Element(“Dictionary”));
您可以使用dynamic
关键字或在.NET4.0中动态创建和操作动态对象。dynamic关键字非常强大
有关如何在数据访问层中成功使用它的开源示例,请参见
发件人:
Visual C#2010引入了一种新类型,即动态。该类型是静态类型,但动态类型的对象会绕过静态类型检查。在大多数情况下,它的功能就像它有类型对象一样。在编译时,假定类型为动态的元素支持任何操作
扩展对象代码示例
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());
}
}
}
输出
您可以考虑创建具有属性的简单类型。将匿名类型传递给方法看起来有点糟糕。如果您仍然想查看它,可以尝试以下方法: 虽然,正如我所说: 传递匿名类型或包含匿名类型的集合 类型,作为方法的参数,可以将参数声明为 输入对象。然而,这样做违背了强类型的目的。 如果必须存储查询结果或将其传递到方法外部 边界,考虑使用普通的命名结构或类来代替 匿名类型
为什么不使用字典呢?那么您可以使用简单的字符串键,或者使用文档中的唯一属性作为键。例如:
public void FooMethod(Dictionary<String, XDocument> docDictionary)
{
var doc1 = docDictionary["parametersXDoc"];
var doc2 = docDictionary["criteriaXDoc"];
blah blah blah
}
public void方法(Dictionary-docDictionary)
{
var doc1=docDictionary[“参数sxdoc”];
var doc2=docDictionary[“criteriaXDoc”];
废话废话
}
干杯,
克丽丝。字典怎么样?键可以是文档的名称是的,您可以使用
动态
关键字。见下面的答案。关于匿名类型,方法签名在另一个方法上看起来如何?您必须使用动态..'params'在C#+1中是一个保留字说得好,我正试图为动态数量的文档想出一个解决方案,但这不是问题您是正确的。这是可能的。然而,我的问题的目的也是为了找出答案……我认为“字典docDictionary”可能比写一堂课花的时间要少!!;)