Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method to handle objects with properties in common, but different object types

Tags:

c#

asp.net

I have a large collection of automatically generated objects. Although they are all of different, non-related classes, all of the objects share some basic properties (name, id, etc.). I do not control the generation of these objects, so unfortunately I cannot take the ideal approach of implementing an interface. I would like to create a method in which I pass an arbitrary one of these objects and do something using these common properties.

The general idea would be something like:

someObj a = new someObj();
a.name = "sara";
diffObj b = new diffObj();
b.name = "joe";
string phrase = string.Format("I am with {0} and {1}", 
    getName(a), getName(b));

private string getName(object anyObjWithName)
{
    return anyObjWithName.name;
}

though naturally this does not work.

I thought a generic method might hold the answer, but the only way I can see to call it with the current type is using genericMethod.Invoke , which still carries the same issue of not being able to resolve the properties of the passed object in the method. This is unlike Calling generic method with a type argument known only at execution time or How to call generic method with a given Type object? where only the type, or properties of the type, are used in the method, as opposed to properties of the object.

I am aware that this would be (very) prone to error, but I can guarantee that all objects encountered will have the common properties being manipulated.

like image 296
MaxPRafferty Avatar asked Aug 12 '13 19:08

MaxPRafferty


People also ask

What are properties and methods of an object?

In most cases, methods are actions and properties are qualities. Using a method causes something to happen to an object, while using a property returns information about the object or causes a quality about the object to change.

What are methods as they relate to objects?

A method defines the behavior of the objects that are created from the class. Another way to say this is that a method is an action that an object is able to perform.

What operator do you use to access the methods and properties of an object?

Object properties and methods can be accessed using dot notation or [ ] bracket.

What are properties and methods for objects in JavaScript?

Object MethodsMethods are actions that can be performed on objects. Object properties can be both primitive values, other objects, and functions. An object method is an object property containing a function definition. JavaScript objects are containers for named values, called properties and methods.


2 Answers

I can guarantee that all objects encountered will have the common properties being manipulated

If that's the case, you can use dynamic:

private string getName(dynamic anyObjWithName)
{
    return anyObjWithName.name;
}

Be aware that using any object that does not have a name property will not fail until run-time.

If you want to add a little bit of safety you can catch the RuntimeBinderException that gets thrown if the property does not exist:

private string getName(dynamic anyObjWithName)
{
    try {
        return anyObjWithName.name;
    }
    catch(RuntimeBinderException) {
        return "{unknown}";
    }
}
like image 56
D Stanley Avatar answered Sep 22 '22 17:09

D Stanley


If you're unhappy with the performance using dynamic as mentioned by D Stanley, you could always try FastMember.

All you need to know to start using it is pretty much shown in the first 2 code examples.

like image 45
cookie Avatar answered Sep 24 '22 17:09

cookie