Home All Groups Group Topic Archive Search About

Dynamically using object methods

Author
18 Dec 2008 10:28 PM
tshad
I have a function that uses goes through all the properties in an object and
sets the value to null.  The problem is that it uses a method in the
property to set it.  At the moment I just do:

        private void SetPropertiesToNull(object objectToInspect)
        {
            Type objectType = objectToInspect.GetType();

            PropertyInfo[] properties = objectType.GetProperties();

            foreach (PropertyInfo property in properties)
            {
                // Set each property to null

                property.SetValue(objectToInspect, null, null);
            }
        }


But I need to change it so that it does something like:

      objectToInspect.SetValuetoNull(property.Name)

but this won't work as it doesn't know what type of object it is and I would
have to do something like:

      ((SubjectObject)objectToInspect).SetValuetoNull(property.Name)

I can get the "SubjetObject" from objectToInspect.GetInfo(), but can't
figure out how to use that dynamically to cast objectToInspect.

Is there a way to do this?

Thanks,

Tom

Author
18 Dec 2008 11:15 PM
Anthony Jones
Show quote Hide quote
"tshad" <t**@dslextreme.com> wrote in message
news:OBvrk$VYJHA.5336@TK2MSFTNGP02.phx.gbl...
>I have a function that uses goes through all the properties in an object
>and sets the value to null.  The problem is that it uses a method in the
>property to set it.  At the moment I just do:
>
>        private void SetPropertiesToNull(object objectToInspect)
>        {
>            Type objectType = objectToInspect.GetType();
>
>            PropertyInfo[] properties = objectType.GetProperties();
>
>            foreach (PropertyInfo property in properties)
>            {
>                // Set each property to null
>
>                property.SetValue(objectToInspect, null, null);
>            }
>        }
>
>
> But I need to change it so that it does something like:
>
>      objectToInspect.SetValuetoNull(property.Name)
>

Why?

> but this won't work as it doesn't know what type of object it is and I
> would have to do something like:
>
>      ((SubjectObject)objectToInspect).SetValuetoNull(property.Name)
>
> I can get the "SubjetObject" from objectToInspect.GetInfo(), but can't
> figure out how to use that dynamically to cast objectToInspect.
>
> Is there a way to do this?
>

If you really do need to then you should ensure all objects that require
this functionality implement an interface that contains the SetValueToNull
(although I would a more flexiable SetValue)

public interface IDynamicProperties
{
    void SetValue(string name, object value);
    object GetValue(string name);
}

public class MyBase : IDynamicProperties
{
    void IDynamicProperties.SetValue(string name, object value)
    {
         Type t = this.GetType();
          t.GetProperty(name).SetValue(this, value, null);
    }

    object IDynamicProperties.GetValue(string name)
    {
        Type t. = this.GetType()
         return t.GetProperty(name).GetValue(this, null);
    }
}

Now you can set a property to null using:-

IDynamicProperties o = objectToInspect;
o.SetValue("somename", null);

--
Anthony Jones - MVP ASP/ASP.NET
Are all your drivers up to date? click for free checkup

Author
18 Dec 2008 11:29 PM
tshad
Show quote Hide quote
"Anthony Jones" <AnthonyWJo***@yadayadayada.com> wrote in message
news:%23P1yAaWYJHA.684@TK2MSFTNGP04.phx.gbl...
> "tshad" <t**@dslextreme.com> wrote in message
> news:OBvrk$VYJHA.5336@TK2MSFTNGP02.phx.gbl...
>>I have a function that uses goes through all the properties in an object
>>and sets the value to null.  The problem is that it uses a method in the
>>property to set it.  At the moment I just do:
>>
>>        private void SetPropertiesToNull(object objectToInspect)
>>        {
>>            Type objectType = objectToInspect.GetType();
>>
>>            PropertyInfo[] properties = objectType.GetProperties();
>>
>>            foreach (PropertyInfo property in properties)
>>            {
>>                // Set each property to null
>>
>>                property.SetValue(objectToInspect, null, null);
>>            }
>>        }
>>
>>
>> But I need to change it so that it does something like:
>>
>>      objectToInspect.SetValuetoNull(property.Name)
>>
>
> Why?

Because I need to set these values to null using a method in our various
classes.

objectToInspect will show only the normal "object" methods: Equals,
GetHashCode,GetType and ToString.  When I cast it, it shows all the methods.

Show quoteHide quote
>
>> but this won't work as it doesn't know what type of object it is and I
>> would have to do something like:
>>
>>      ((SubjectObject)objectToInspect).SetValuetoNull(property.Name)
>>
>> I can get the "SubjetObject" from objectToInspect.GetInfo(), but can't
>> figure out how to use that dynamically to cast objectToInspect.
>>
>> Is there a way to do this?
>>
>
> If you really do need to then you should ensure all objects that require
> this functionality implement an interface that contains the SetValueToNull
> (although I would a more flexiable SetValue)
>
Unfortunately, I have no control over the classes.  I didn't create them.

But I can get the class type with the GetType() method.  Just don't know how
to apply it to the object.  Since I only have 3 types of objects at the
moment, I could aways set up a switch statement using the result from
GetType() and then cast each one.

But I would prefer to do it dynamically if I can.

Thanks,

Tom
Show quoteHide quote
> public interface IDynamicProperties
> {
>    void SetValue(string name, object value);
>    object GetValue(string name);
> }
>
> public class MyBase : IDynamicProperties
> {
>    void IDynamicProperties.SetValue(string name, object value)
>    {
>         Type t = this.GetType();
>          t.GetProperty(name).SetValue(this, value, null);
>    }
>
>    object IDynamicProperties.GetValue(string name)
>    {
>        Type t. = this.GetType()
>         return t.GetProperty(name).GetValue(this, null);
>    }
> }
>
> Now you can set a property to null using:-
>
> IDynamicProperties o = objectToInspect;
> o.SetValue("somename", null);
>
> --
> Anthony Jones - MVP ASP/ASP.NET
>
Author
19 Dec 2008 8:15 AM
Anthony Jones
Show quote Hide quote
"tshad" <t**@dslextreme.com> wrote in message
news:ues4rhWYJHA.5108@TK2MSFTNGP05.phx.gbl...
> "Anthony Jones" <AnthonyWJo***@yadayadayada.com> wrote in message
> news:%23P1yAaWYJHA.684@TK2MSFTNGP04.phx.gbl...
>> "tshad" <t**@dslextreme.com> wrote in message
>> news:OBvrk$VYJHA.5336@TK2MSFTNGP02.phx.gbl...
>>>I have a function that uses goes through all the properties in an object
>>>and sets the value to null.  The problem is that it uses a method in the
>>>property to set it.  At the moment I just do:
>>>
>>>        private void SetPropertiesToNull(object objectToInspect)
>>>        {
>>>            Type objectType = objectToInspect.GetType();
>>>
>>>            PropertyInfo[] properties = objectType.GetProperties();
>>>
>>>            foreach (PropertyInfo property in properties)
>>>            {
>>>                // Set each property to null
>>>
>>>                property.SetValue(objectToInspect, null, null);
>>>            }
>>>        }
>>>
>>>
>>> But I need to change it so that it does something like:
>>>
>>>      objectToInspect.SetValuetoNull(property.Name)
>>>
>>
>> Why?
>
> Because I need to set these values to null using a method in our various
> classes.
>
> objectToInspect will show only the normal "object" methods: Equals,
> GetHashCode,GetType and ToString.  When I cast it, it shows all the
> methods.
>

What I meant was Why do you need to call a method on the object?
Why can't you use a static utility function that takes the object as a
parameter?

public static class Utils
{

public static void SetPropertyValue(object target,
                                string name, object value)
{
         Type t = target.GetType();
          t.GetProperty(name).SetValue(target, value, null);
}
}
....

Utils.SetPropertyValue(objectToInspect, "someproperty", null);

Show quoteHide quote
>>
>>> but this won't work as it doesn't know what type of object it is and I
>>> would have to do something like:
>>>
>>>      ((SubjectObject)objectToInspect).SetValuetoNull(property.Name)
>>>
>>> I can get the "SubjetObject" from objectToInspect.GetInfo(), but can't
>>> figure out how to use that dynamically to cast objectToInspect.
>>>
>>> Is there a way to do this?
>>>
>>
>> If you really do need to then you should ensure all objects that require
>> this functionality implement an interface that contains the
>> SetValueToNull (although I would a more flexiable SetValue)
>>
> Unfortunately, I have no control over the classes.  I didn't create them.
>
> But I can get the class type with the GetType() method.  Just don't know
> how to apply it to the object.  Since I only have 3 types of objects at
> the moment, I could aways set up a switch statement using the result from
> GetType() and then cast each one.
>
> But I would prefer to do it dynamically if I can.
>

Since you don't have control over the classes, how were you expecting to get
them to implement a SetValueToNull method in the first place?

The static method I provide above is the only sensible approach.

--
Anthony Jones - MVP ASP/ASP.NET

Bookmark and Share