|
ms
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Dynamically using object methodssets 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
Show quote
Hide quote
"tshad" <t**@dslextreme.com> wrote in message Why?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) > > but this won't work as it doesn't know what type of object it is and I If you really do need to then you should ensure all objects that require > 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? > 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
Show quote
Hide quote
"Anthony Jones" <AnthonyWJo***@yadayadayada.com> wrote in message Because I need to set these values to null using a method in our various 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? 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 > Unfortunately, I have no control over the classes. I didn't create them.>> 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) > 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 >
Show quote
Hide quote
"tshad" <t**@dslextreme.com> wrote in message What I meant was Why do you need to call a method on the object?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. > 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 >> Since you don't have control over the classes, how were you expecting to get >>> 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. > 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
Other interesting topics
Search all nodes using Linq
installer class failure Transition from C# to VB.NET xml Second message loop a Windows Forms application? delegate parameter - can I do this? Filtering a XmlNodeList a simple linq query A dialog in a different thread than its parent? How to stop Invoked code running immediately after ShowDialog() closes |
|||||||||||||||||||||||