Question

I need a function with following signature in C# 4.0, I am lost where to start :

public static object SetStringPropertiesOnly(object obj)
{

   //iterate all properties of obj
   //if the type of the property is string,
   //return obj
}

and eventually I want to use this function for my several objects derived from different classes:

myClass1 obj1 = new myClass1 ();
myClass2 obj2 = new myClass2 ();
.....
.....
obj1 = SetStringPropertiesOnly(obj1);
obj2 = SetStringPropertiesOnly(obj2);

So the type of the objects are dynamic here.

Is such a method possible?.

Thanks.

Was it helpful?

Solution 2

I suppose you want to return the object itself. However you should understand that the original object will also be changed.

    public static object SetStringPropertiesOnly(object obj)
    {
        var properties = obj.GetType().GetProperties();
        var strings = properties.Where(p=>p.PropertyType == typeof(string);
        foreach(PropertyInfo property in strings)
        {
            property.SetValue(obj, "Value");
        }
        return obj;
    }

My approach would be to make an extension method and return void, since the object would be changed. I also would add the wished string as a parameter.

    public static void SetStringProperties(this object obj, string value)
    {
        var properties = obj.GetType().GetProperties();
        var strings = properties.Where(p=>p.PropertyType == typeof(string);
        foreach(PropertyInfo property in strings)
        {
            property.SetValue(obj, value);
        }
        return obj;
    }

You can call the extension method like this:

obj.SetStringProperties("All strings will have this value");

By the way, the fact that you need to do this might be considered a "bad smelling code". Reconsider this design if you can.

OTHER TIPS

public static object SetStringPropertiesOnly(object obj)
{
  //Get a list of properties where the declaring type is string
  var stringProps = obj.GetType().GetProperties().Where(x => x.PropertyType == typeof(string)).ToArray();
  foreach (var stringProp in stringProps)
  {
    // If this property exposes a setter...
    if (stringProp.SetMethod != null)
    {
      //Do what you need to do
      stringProp.SetValue(obj, "value", null);
    }
  }
  //What do you want to return?
  return obj;
}

Consider changing your method signature to accept a value parameter and also change object obj to be ref, you don't need to return your object then.

Aint hard using reflection. And we can also do it as object extension (looks cute when you use it):

public static class ObjectExtensions
{
    public static T SetStringPropertiesOnly<T>(this T obj) where T : class
    {
        var fields = obj.GetType().GetProperties();

        foreach (var field in fields)
        {
            if (field.PropertyType == typeof (string))
            {
                field.SetValue(obj, "blablalba", null); //set value or do w/e your want
            }
        }
        return obj;

    }
}

and usage:

var obj = someObject.SetStringPropertiesOnly();

You could use a common interface, something in the lines of "IBulkStringEditable". The interface should contain a method "void SetStrings()". Then all your classes have to implement this interface and the SetStrings method, where every class has different contents for SetStrings, depending on the string properties it has and the values you want them to have. Then modify the SetStringPropertiesOnly function in this manner:

public static IBulkStringEditable SetStringPropertiesOnly(IBulkStringEditable obj)
{
    obj.SetStrings();
    return obj;
}

simply u can use dynamic in ur method parameter signature like that--->

public static object SetStringPropertiesOnly(dynamic obj)
{
    // proceed
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top