Dynamically using object methods

T

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
 
A

Anthony Jones

tshad said:
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);
 
T

tshad

Anthony Jones said:
tshad said:
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.
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
 
A

Anthony Jones

tshad said:
Anthony Jones said:
tshad said:
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);
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top