c# - Refer Object's Property by string -


Is it possible to call the property's property by its key name?

I want to update the user's profile Data in the cookie, so I have the following code

  bool propertyChanged = false; UserProfile ThisUserProfile = context.UserProfiles.Single (U = & gt; u.Id == int.Parse (GetCookieValue (UserIdString)); If (this user profile.property1! = GetCookieValue ("Property1")) {ThisUserProfile.Property1 = GetCookieValue ("property1") PropertyChanged = true} (ThisUserProfile.Property2! = GetCookieValue ("Property2")) {ThisUserProfile.Property2 = GetCookieValue ( "property 2") property Chenjed = true} if (it Usrprofailkproprti 3! = GetCookieValue ( "Property3")) {ThisUserProfile.Property3 = GetCookieValue ( "Property3") PropertyChanged = true} // and many more properties ..... if (property changed) references Save Changes ();  

Is it possible to type the method to update the property object as the following?

  Zero UpdateIfChanged {String key} {ThisUserProfile [key] = GetCookieValue ("key") property change = true}  

Then I create a string Can I use an advanced option loop to do everything at once?

You can use some reflection.

  Public static zero SetValue (object unit, string propertyName, object value) {try {PropertyInfoPie = Entity. Gettype () GetProperty (propertyName); Type T = Null. GETUnderlyingType (pi.PropertyType) ?? Pi.PropertyType; Item secured value = (value == empty)? Faucet: Convert.Change Type (Value, T); Pi.SetValue (unit, secure value, blank); } Hold (before the illegal cutoffation) {// Casting casting between different assemblies ... Try {PropertyInfo pi = entity.GetType (). GetProperty (propertyName); Type T = Null. GETUnderlyingType (pi.PropertyType) ?? Pi.PropertyType; Item secured value = (value == empty)? Faucet: catalyst. Createinstance (T, Value); Pi.SetValue (unit, secure value, blank); } Hold {}} hold {} return; }  

Then use this method like this:

  string [] userProperties = { "username", "email", "address"} ; Foreach (string in the UserProperties) {// You can set your application, so the cookie property names are the same as your unit ... SetValue (ThisUserProfile, s, value); }   

for your needs try / catch block SetValue also adapted to you ThisUserProfile , So I could not fully test that it is functional in your specific case.

You should know that the reflection is slow, so use it only when you need it.

Since you are not using are using entity framework and inform you change tracker normal set accessor (that ongoing changes in unit) Before the changes ( SaveChanges ), you can set the entity's state property to EntityState.Modified .

GetValue may also be useful, if you plan to use the reflection to get value from another object then

  Public static object GetValue (object unit, string propertyName) {try {return entity GetType (). GetProperty (PropertyName, BindingFlag.Engore Case | BindingFlag.public | BindingFlag.instance) .GetValue (unit, empty); } Hold {return tap; }}  

Comments

Popular posts from this blog

import - Python ImportError: No module named wmi -

Editing Python Class in Shell and SQLAlchemy -

c# - MySQL Parameterized Select Query joining tables issue -