Home  >  Article  >  Backend Development  >  How to modify an instance of value through object property name in C#

How to modify an instance of value through object property name in C#

黄舟
黄舟Original
2017-09-18 11:01:293599browse

Excerpted from: csdn

Assigning a value to an object property can be done through PropertyInfo.SetValue(), but it should be noted that the type of the value must be consistent with the property.

Two ways to create an object instance:

1.


var obj = Assembly.Load("AssemblyName").CreateInstance("AssemblyName"+"ClassFullName");

2.

 
var
 obj = Activator.CreateInstance(ClassType);

Create the instance When , you can now assign a value to an attribute of the current instance. First, obtain the attribute to be assigned.


var property = obj.GetType().GetProperty("PropertyName");//此时可以使用GetProperty获取属性数组,循环进行赋值,这里主要讲解类型问题。

Assignment can be done through the PropertyInfo.SetValue() method, see MSDN for details.

Case 1, the attribute type is a known type, for example: int


int value=500;
property.SetValue(obj,value,null);

It should be noted here that the value type must be consistent with the attribute type, otherwise TargetException will be thrown.

Case 2, the attribute type is a known type, and the original value is another type. For example: the target type is int and the value is string


string value="500";
property.SetValue(obj,int.TryParse(value),null);//类型转换。

The first two cases are very simple. Sometimes the business will be more complicated and the target type is not sure, which requires the program to run. judge.

Case 3, the attribute type is an unknown non-generic type, the target type is not sure, how to perform type conversion.


object value="500";
property.SetValue(obj,Convert.ChangeType(value,property.PropertyType),null);//类型转换。

This will solve most problems.

I don’t know if you have noticed that I emphasized non-generic type in the third case. Doesn’t generic type work?
Yes. If you just use the Convert.ChangeType() method and the type conversion still reports an error, look at the following code first.

Even if the target type and the value type are consistent, an error will still be reported when converting through Convert.ChangeType().
To solve this problem, we must first convert the attribute value type to the base type and then perform Convert conversion. Looking at the code, no error will be reported when using Convert.ChangeType() to convert a nullable type.
Add some basic judgment verification, and the code will be more complete.


if (!property.PropertyType.IsGenericType)
            {                //非泛型
                property.SetValue(obj, string.IsNullOrEmpty(value) ? null : Convert.ChangeType(value, property.PropertyType), null);
            }            else
            {                //泛型Nullable<>
                Type genericTypeDefinition = property.PropertyType.GetGenericTypeDefinition();                
                if (genericTypeDefinition == typeof(Nullable<>))
                {
         property.SetValue(obj, string.IsNullOrEmpty(value) ? null : Convert.ChangeType(value, Nullable.GetUnderlyingType(property.PropertyType)), null);
                }
            }

The above is the detailed content of How to modify an instance of value through object property name in C#. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn