Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing data between objects in c#

I'm developing a Desktop Applicaiton which is akin to a plugin based system. I have a client module, that will load a DLL containing a 'Machine' object. Thereafter the 'Machine' object is manipulated and used as per well defined interfaces.

The tricky part is that the DLL containing the 'Machine' object is generated on the fly by using another program. At it's core, the DLL generating application accepts user input, generates classes, in c# code files (which contain fields specified by the user, of which I don't have any prior knowledge of) and compiles those classes to a DLL (machine.dll file).

The client program picks up this dll, dynamically loads it and then operates on this machine bject.

I am facing a lot of trouble modeling a solution to address the problem of passing data between the Machine object and the Client program, essentially because I dont know what data is contained in the machine object.

The client program is to do the following things.

-- Load the dll and instantiate the 'machine' object. -- Call a series of functions in the 'machine' object, that are known to the client via an interface. -- Extract various variables from the 'machine' object and display it to the user.

I am not able to perform the last step.

Note: I have programmed a trivial solution where the meta-data about the fields is generated by the dll generating program and stored in xml files. The client program uses these xml files to get information about the fields stored in the machine object. It then uses reflection on the machine object to access the fields of the object.

I feel this is cumbersome and slow. Are there any patterns or methods to this kind of stuff??

like image 919
Raghavendra Kumar Avatar asked Dec 14 '12 04:12

Raghavendra Kumar


People also ask

How to pass an object in C?

There are two ways to pass data with C and C++: by value and by reference. By value means that a temporary copy of the argument is passed to the called function or procedure. By reference means that the address of the argument is passed.

How to pass object through function?

To pass an object as an argument we write the object name as the argument while calling the function the same way we do it for other variables. Syntax: function_name(object_name); Example: In this Example there is a class which has an integer variable 'a' and a function 'add' which takes an object as argument.

Which is better pass-by-value or pass by reference?

Pass-by-references is more efficient than pass-by-value, because it does not copy the arguments. The formal parameter is an alias for the argument. When the called function read or write the formal parameter, it is actually read or write the argument itself.

What is pass-by-value in C++ with example?

When you use pass-by-value, the compiler copies the value of an argument in a calling function to a corresponding non-pointer or non-reference parameter in the called function definition. The parameter in the called function is initialized with the value of the passed argument.


2 Answers

A solution that came to mind when I read this was to make use of the built-in support for Attributes in C#. An attribute is a way of tagging a property, field, method, class, etc with some additional meta-data that is then used by some other class, for example during Serialization. You will see it there most often.

I had an application that I was building that needed to be able to take an IEnumerable collection of objects and output some data to a file based on user selected choices. I created an attribute class that gave me the ability to read the choices via reflection, and act as directed. Let me show you the example:

First the attribute class:

[System.AttributeUsage(AttributeTargets.Property)]
class ExportOptionsAttribute : System.Attribute
{
    public string Header { get; set; }
    public string FormatString { get; set; }
    public bool Export { get; set; }
    public int Order { get; set; }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="header"></param>
    public ExportOptionsAttribute(string header) : this (header, null, true)
    {

    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="header"></param>
    /// <param name="formatString"></param>
    /// <param name="export"></param>
    public ExportOptionsAttribute(string header, string formatString, bool export)
    {
        this.Header = header;
        this.FormatString = formatString;
        this.Export = export;
        this.Order = 0;
    }
}

With this class defined like so, I could decorate my data class properties like this (actual properties changed so as to not get lost on the business jargon):

public sealed class PartsOrder
{
    /// <summary>
    /// 
    /// </summary>  
    [ExportOptions("Customer Name", Order=0)]
    public string CustomerName { get; set; }

    /// <summary>
    /// 
    /// </summary>
    [ExportOptions("Catalog Name", Order = 1)]
    public string Catalog Name { get; set; }

    /// <summary>
    /// 
    /// </summary>
    [ExportOptions("Unit", Order = 2)]
    public string Unit { get; set; }

    /// <summary>
    /// 
    /// </summary>
    [ExportOptions("Component", Order = 3)]
    public string Component { get; set; }

    /// <summary>
    /// 
    /// </summary>
    [ExportOptions("Delivery Point", Order = 4)]
    public string DeliveryPoint { get; set; }

    /// <summary>
    /// 
    /// </summary>  
    [ExportOptions("Order Date", Order = 5)]
    public string OrderDate { get; set; }
}

So then in my export routine, instead of hard-coding the property names, which are variable, or passing a complex data structure around which contained the information on which fields to show or hide and what the ordering was, I just ran the following code, using reflection, to loop the properties and output their values, to a CSV file in this case.

StringBuilder outputDoc = new StringBuilder();

// loop through the headers in the attributes
// a struct which decomposes the information gleaned from the attributes
List<OrderedProperties> orderedProperties = new List<OrderedProperties>();

// get the properties for my object
PropertyInfo[] props =
    (typeof(PartsOrder)).GetProperties();

// loop the properties
foreach (PropertyInfo prop in props)
{
    // check for a custom attribute
    if (prop.GetCustomAttributesData().Count() > 0)
    {
        foreach (object o in prop.GetCustomAttributes(false))
        {
            ExportOptionsAttribute exoa = o as ExportOptionsAttribute;

            if (exoa != null)
            {
                orderedProperties.Add(new OrderedProperties() { OrderByValue = exoa.Order, PropertyName = prop.Name, Header = exoa.Header, Export = exoa.Export });
            }
        }
    }
}

orderedProperties = orderedProperties.Where(op => op.Export == true).OrderBy(op => op.OrderByValue).ThenBy(op => op.PropertyName).ToList();

foreach (var a in orderedProperties)
{
    outputDoc.AppendFormat("{0},", a.Header);
}

// remove the trailing commma and append a new line
outputDoc.Remove(outputDoc.Length - 1, 1);
outputDoc.AppendFormat("\n");


var PartsOrderType = typeof(PartsOrder);

//TODO: loop rows
foreach (PartsOrder price in this.Orders)
{
    foreach (OrderedProperties op in orderedProperties)
    {
        // invokes the property on the object without knowing the name of the property
        outputDoc.AppendFormat("{0},", PartsOrderType.InvokeMember(op.PropertyName, BindingFlags.GetProperty, null, price, null));
    }

    // remove the trailing comma and append a new line
    outputDoc.Remove(outputDoc.Length - 1, 1);
    outputDoc.AppendFormat("\n");
}

The code for the OrderedProperties struct is here:

struct OrderedProperties
{
    /// <summary>
    /// 
    /// </summary>
    public int OrderByValue;
    /// <summary>
    /// 
    /// </summary>
    public string PropertyName;
    /// <summary>
    /// 
    /// </summary>
    public string Header;
    /// <summary>
    /// 
    /// </summary>
    public bool Export;
}

As you can see, the logic to extract the property values is completely ignorant of the structure of the class. All it does is find the properties that are decorated with the attribute I created, and use that to drive the processing.

I hope this all makes sense, and if you need more help or clarification, please feel free to ask.

like image 129
Maurice Reeves Avatar answered Sep 23 '22 01:09

Maurice Reeves


You can also pick up ZeroMQ, which is a lightweight MQ software, enabled the communication between applications. ZeroMQ includes only 1 dll, and you can embedded in any of your applications.

ZeroMQ has all kind of client, include C, C++, .NET, Python, Java,Ruby ,and can run in both Windows/Linux/OSX..

like image 34
ray_linn Avatar answered Sep 22 '22 01:09

ray_linn