Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET 4.0 / C# - Creating objects on the fly

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

I want to pass them all to a method as an argument. I could pass them as an array, but then I would need to know the position/index of the XDocument I require - this seems messy.

Is it possible to create a temporary wrapper object on the fly (with properties) that point to each of the XDocument variables and pass this instead?

like image 661
user559142 Avatar asked Jan 26 '12 09:01

user559142


People also ask

What is the 4th version of NET Framework?

NET Framework 4.0 có khả năng đơn giản hóa viết ứng dụng bằng việc cung cấp các thành phần có sẵn. NET Framework 4.0 về máy để khám phá và trải nghiệm nhiều chức năng hấp dẫn. 1. Giới thiệu về .Net frame work 4.0 2. Tính năng chính của Microsoft.NET Framework 3. So sánh NET Farmework 4.0 với phiên bản khác

What is the purpose of the Microsoft NET Framework 4 web installer?

The Microsoft .NET Framework 4 web installer package downloads and installs the .NET Framework components required to run on the target machine architecture and OS. An Internet connection is required during the installation. The Client Profile is used to run most client applications that target the .NET Framework 4.

What is NET Framework 4 redistributable package?

The Microsoft .NET Framework 4 redistributable package installs the .NET Framework runtime and associated files that are required to run and develop applications to target the .NET Framework 4. The .NET Framework is Microsoft's comprehensive and consistent programming model for building applications that have visually stunning user experiences, ...

What is the Microsoft NET Framework 4 client profile?

The Microsoft .NET Framework 4 Client Profile provides a subset of features from the .NET Framework 4. The Client Profile is designed to run client applications and to enable the fastest possible deployment for Windows Presentation Foundation (WPF) and Windows Forms technology.


1 Answers

You can create and manipulate dynamic objects on the fly, using the dynamic keyword or ExpandoObject in .NET4.0. The dynamic keyword is extremely powerful.

An OpenSource example of how it can be used successfully in a data-access layer can be found in the PetaPoco micro-ORM.

From MSDN:

Visual C# 2010 introduces a new type, dynamic. The type is a static type, but an object of type dynamic bypasses static type checking. In most cases, it functions like it has type object. At compile time, an element that is typed as dynamic is assumed to support any operation

ExpandoObject Code example

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

Output

enter image description here

like image 196
Dr. Andrew Burnett-Thompson Avatar answered Sep 24 '22 12:09

Dr. Andrew Burnett-Thompson