Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在运行时操作用户定义类的属性?_C#_Reflection_Properties_Helpermethods - Fatal编程技术网

C# 如何在运行时操作用户定义类的属性?

C# 如何在运行时操作用户定义类的属性?,c#,reflection,properties,helpermethods,C#,Reflection,Properties,Helpermethods,我有一个巨大的用户定义的类,它有很多属性,其中一些属性必须设置为特定的值 更具体地说,此用户定义的类中所有类型为字符串的公共属性必须在执行结束时“清空” 所以我一个接一个地访问了所有公共属性(比如300个属性),并为它们分配了300行代码。我为解决这个问题所做的,做了我需要的,但当然不能满足我 因此,我决定在C#中编写一个助手方法(作为扩展方法),它迭代对象实例的属性并动态访问/操作它们。我在这里看到了一些关于迭代属性的类似问题,但到目前为止没有看到任何关于更改属性值的问题。以下是我尝试过的:

我有一个巨大的用户定义的类,它有很多属性,其中一些属性必须设置为特定的值

更具体地说,此用户定义的类中所有类型为字符串的公共属性必须在执行结束时“清空”

所以我一个接一个地访问了所有公共属性(比如300个属性),并为它们分配了300行代码。我为解决这个问题所做的,做了我需要的,但当然不能满足我

因此,我决定在C#中编写一个助手方法(作为扩展方法),它迭代对象实例的属性并动态访问/操作它们。我在这里看到了一些关于迭代属性的类似问题,但到目前为止没有看到任何关于更改属性值的问题。以下是我尝试过的:

public static void SetDefaultStringValues(this Object myObject)
{
    PropertyInfo[] aryProperties = entityObject.GetType().GetProperties();

    foreach (object property in aryProperties)
    {
        if (property is String) 
        {
            //property = String.Empty;
        }
    }
}

注释部分必须是我设置变量的那一行,但这并不是一个真正成功的故事:)我将感谢任何帮助。

使用PropertyInfo.SetValue方法,在使用PropertyInfo.SetValue方法中有更多关于这一点的内容,在中你需要不同的语法来检查属性类型;此外,您应该检查属性是否具有公共setter

if (property.PropertyType == typeof(string) && property.GetSetMethod() != null)
    property.SetValue(entityObject, "");

您需要使用不同的语法来检查属性类型;此外,您应该检查属性是否具有公共setter

if (property.PropertyType == typeof(string) && property.GetSetMethod() != null)
    property.SetValue(entityObject, "");

您可以使用
System.ComponentModel
PropertyDescriptor
进行惰性编码。假设您希望迭代公共实例方法(而不是静态方法),可以尝试以下示例:

测试等级:

SetDefaultStringValues()扩展方法:

公共静态字符串SetDefaultStringValues(此TestClass TestClass)
{
StringBuilder returnLogBuilder=新建StringBuilder();
//#获取testClass实例的所有属性
PropertyDescriptorCollection-PropDescollection=TypeDescriptor.GetProperties(testClass);
//#迭代属性集合
foreach(PropDescollection中的PropertyDescriptor属性)
{
字符串名称=property.name;
类型t=property.PropertyType;//#获取属性类型
object value=property.GetValue(testClass);//#获取属性的值(成员变量持有的值)
if(t==typeof(string))/#如果属性的类型为string,值为null
{
property.SetValue(testClass,String.Empty);//#属性的设置值(Set成员变量)

value=String.Empty;//#您可以使用
System.ComponentModel
PropertyDescriptor
进行惰性编码。假设您希望迭代公共实例方法(而不是静态),您可以尝试以下示例:

测试等级:

SetDefaultStringValues()扩展方法:

公共静态字符串SetDefaultStringValues(此TestClass TestClass)
{
StringBuilder returnLogBuilder=新建StringBuilder();
//#获取testClass实例的所有属性
PropertyDescriptorCollection-PropDescollection=TypeDescriptor.GetProperties(testClass);
//#迭代属性集合
foreach(PropDescollection中的PropertyDescriptor属性)
{
字符串名称=property.name;
类型t=property.PropertyType;//#获取属性类型
object value=property.GetValue(testClass);//#获取属性的值(成员变量持有的值)
if(t==typeof(string))/#如果属性的类型为string,值为null
{
property.SetValue(testClass,String.Empty);//#属性的设置值(Set成员变量)
value=String.Empty;//#
public class TestClass
{
    private int mIntMemeber = 0;                    // # to test int type   
    private string mStringMember = "abc";           // # to test string type (initialized)
    private string mNullStringMember = null;        // # to test string type (null)

    private static string mStaticNullStringMember;  // # to test string type (static)

    // # Defining properties for each member
    public int IntMember                            
    {
        get { return mIntMemeber; }
        set { mIntMemeber = value; }                
    }

    public string StringMember                      
    {
        get { return mStringMember; }
        set { mStringMember = value; }
    }

    public string NullStringMember
    {
        get { return mNullStringMember; }
        set { mNullStringMember = value; }
    }

    public static string StaticNullStringMember
    {
        get { return mStaticNullStringMember; }
        set { mStaticNullStringMember = value; }
    }
}
public static string SetDefaultStringValues(this TestClass testClass)   
{
    StringBuilder returnLogBuilder = new StringBuilder();
    // # Get all properties of testClass instance
    PropertyDescriptorCollection propDescCollection = TypeDescriptor.GetProperties(testClass);
    // # Iterate over the property collection
    foreach (PropertyDescriptor property in propDescCollection)
    {
        string name = property.Name;
        Type t = property.PropertyType; // # Get property type
        object value = property.GetValue(testClass); // # Get value of the property (value that member variable holds)

        if (t == typeof(string)) // # If type of propery is string and value is null
        {
            property.SetValue(testClass, String.Empty); // # Set value of the property (set member variable)
            value = String.Empty; // # <-- To prevent NullReferenceException when printing out
        }

        returnLogBuilder.AppendLine("*****\nName:\t{0}\nType:\t{1}\nValue:\t{2}", name, t.ToString(), value.ToString());
    }

    returnLogBuilder.AppendLine("*****");
    return returnLogBuilder.toString();
}