Tuesday 15 September 2015

c# - Reflection Initialize Property Values of an object Instance Using -


I have an object example in the constructor of the object, I would like to allow the user to pass in a dictionary with which to begin To do, some, if not all the properties of the object Now, instead of using a conditional, what I would like to do, I am using reflection, reflecting the properties contained in the object instance, and if the attribute name, the map for the key in a dictionary, with the associated value In the Update Property Value dictionary.

In working on this, I have the following code, but it does not update the value of my object instance. Would it appreciate some help to do this work?

  Public Zero Starter () {if (Report.GlobalParameters! = Null) {PropertyInfo [] propertyInfos = this.GetType () GetProperties (BindingFlags.Public | BindingFlags.Instance); Foreach (property INFO property in INFO) {if (Report.GlobalParameters.ContainsKey (propertyInfo.Name)) {type type = this.GetType (); PropertyInfo Property = Type Gateproperty (propertyInfo.Name); Property. Set value (report, global parameter [propertyInfo.Name], blank); }}}}}    

First of all, did you attach a debugger to check Is that you or not the most nested if are happening inside? And if you are not coming inside the most nested inside , then you can understand what you're doing, by comparing it to what you're actually doing when you check in the debugger Are you

Second, inside most nested code, if , you can delete the first row, and with property with propertyInfo Change in the third row (which will be the only remaining line when you remove the first two). You already have the property information in the name given in the hand, why are you looking at it again?

Beyond that, it's like you looks should work. In this way, there is disturbance somewhere, which means that you are not going in the right values, or something else that you are not telling us.

What do you have that should help you:

  using the system; Using System.Collections.Generic; Class fu {public int bar {get; Set; } Public Foo (dictionary & lt; string, object & gt; value) {var propertiesin = this. GetType (). GetProperties (); Foreach (different property in property property) {if (values.ContainsKey (property.Name)) {property.SetValue (this, value [property.Name], zero); }}}} Class program {public static zero main (string [] args) {dictionary   

Note that this is your logic and is this help?

No comments:

Post a Comment