Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.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#_Wpf_Reflection_Silverlight 4.0_Dataset - Fatal编程技术网

C# 如何清除动态对象内的值?

C# 如何清除动态对象内的值?,c#,wpf,reflection,silverlight-4.0,dataset,C#,Wpf,Reflection,Silverlight 4.0,Dataset,我正在将数据集转换为动态集合并绑定它,这很好。现在,当我需要向集合添加一个空的新对象时。我尝试的是获取datagrid的ItemsSource并获取列表中的第一个对象。但是它里面有一些价值。如何删除值并使用反射绑定空对象 // gets the Type Type type = list[0].GetType(); // gets public, parameterless constructor ConstructorInfo ci = type.GetConstructor(new Ty

我正在将数据集转换为动态集合并绑定它,这很好。现在,当我需要向集合添加一个空的新对象时。我尝试的是获取datagrid的ItemsSource并获取列表中的第一个对象。但是它里面有一些价值。如何删除值并使用反射绑定空对象

// gets the Type
Type type = list[0].GetType(); 

// gets public, parameterless constructor
ConstructorInfo ci = type.GetConstructor(new Type[0]);

// instantiates the object
object obj = ci.Invoke(new object[0]);
这是我的密码

    IEnumerable<object> collection = this.RetrieveGrid.ItemsSource.Cast<object>();
    List<object> list = collection.ToList();
    //i need to clear the values inside list[0]
    object name = list[0];
    //here i build the properties of the object, now i need to create an empty object using these properties and add it to the list
    PropertyInfo[] pis = list[0].GetType().GetProperties();
IEnumerable collection=this.RetrieveGrid.ItemsSource.Cast();
List=collection.ToList();
//我需要清除列表[0]中的值
对象名称=列表[0];
//这里我构建了对象的属性,现在我需要使用这些属性创建一个空对象并将其添加到列表中
PropertyInfo[]pis=list[0]。GetType().GetProperties();

如果未知类型具有某些已知构造函数,则可以使用反射实例化它

// gets the Type
Type type = list[0].GetType(); 

// gets public, parameterless constructor
ConstructorInfo ci = type.GetConstructor(new Type[0]);

// instantiates the object
object obj = ci.Invoke(new object[0]);
显然,如果没有一个简单的无参数构造函数,这是行不通的。如果知道类构造函数总是采用某个参数,例如整数值,那么可以使用
newtype[]{typeof(int)}
newobject[]{someIntValue}
修改上面的代码段

但这是否会创建“空”对象取决于构造函数的行为



如果要设置某些属性,可以通过调用
type.GetProperties()
返回
PropertyInfos
,然后使用适当的值调用
SetValue

如果未知类型具有某些已知构造函数,则可以使用反射实例化它

// gets the Type
Type type = list[0].GetType(); 

// gets public, parameterless constructor
ConstructorInfo ci = type.GetConstructor(new Type[0]);

// instantiates the object
object obj = ci.Invoke(new object[0]);
显然,如果没有一个简单的无参数构造函数,这是行不通的。如果知道类构造函数总是采用某个参数,例如整数值,那么可以使用
newtype[]{typeof(int)}
newobject[]{someIntValue}
修改上面的代码段

但这是否会创建“空”对象取决于构造函数的行为



如果要设置某些属性,可以通过调用
type.GetProperties()
返回
PropertyInfos
,然后使用适当的值调用
SetValue

如果未知类型具有某些已知构造函数,则可以使用反射实例化它

// gets the Type
Type type = list[0].GetType(); 

// gets public, parameterless constructor
ConstructorInfo ci = type.GetConstructor(new Type[0]);

// instantiates the object
object obj = ci.Invoke(new object[0]);
显然,如果没有一个简单的无参数构造函数,这是行不通的。如果知道类构造函数总是采用某个参数,例如整数值,那么可以使用
newtype[]{typeof(int)}
newobject[]{someIntValue}
修改上面的代码段

但这是否会创建“空”对象取决于构造函数的行为



如果要设置某些属性,可以通过调用
type.GetProperties()
返回
PropertyInfos
,然后使用适当的值调用
SetValue

如果未知类型具有某些已知构造函数,则可以使用反射实例化它

// gets the Type
Type type = list[0].GetType(); 

// gets public, parameterless constructor
ConstructorInfo ci = type.GetConstructor(new Type[0]);

// instantiates the object
object obj = ci.Invoke(new object[0]);
显然,如果没有一个简单的无参数构造函数,这是行不通的。如果知道类构造函数总是采用某个参数,例如整数值,那么可以使用
newtype[]{typeof(int)}
newobject[]{someIntValue}
修改上面的代码段

但这是否会创建“空”对象取决于构造函数的行为


然后,如果要设置某些属性,可以通过调用
type.GetProperties()
返回
propertyInfo
,并使用适当的值调用
SetValue
,对其进行迭代

  • 获取该对象的类型并创建新对象,然后将其添加到 列表,列表[0]

  • 编写函数,传递此对象,获取其类型,清除单个属性, 如果你知道它包含什么属性

  • 获取该对象的类型并创建新对象,然后将其添加到 列表,列表[0]

  • 编写函数,传递此对象,获取其类型,清除单个属性, 如果你知道它包含什么属性

  • 获取该对象的类型并创建新对象,然后将其添加到 列表,列表[0]

  • 编写函数,传递此对象,获取其类型,清除单个属性, 如果你知道它包含什么属性

  • 获取该对象的类型并创建新对象,然后将其添加到 列表,列表[0]

  • 编写函数,传递此对象,获取其类型,清除单个属性, 如果你知道它包含什么属性


  • 调用
    Activator.CreateInstance
    创建新实例。然后使用
    PropertyInfo.SetValue
    将字符串字段设置为空

    Type requiredType = list[0].GetType();
    object instance = Activator.CreateInstance(requiredType);
    PropertyInfo[] pis = requiredType.GetProperties();
    foreach (var p in pis)
    {
        if (p.PropertyType == typeof(string))
        {
            p.SetValue(instance, string.Empty);
        }
    }
    

    请注意,如果类型没有无参数构造函数,则
    Activator.CreateInstance
    会引发异常。

    调用
    Activator.CreateInstance
    以创建新实例。然后使用
    PropertyInfo.SetValue
    将字符串字段设置为空

    Type requiredType = list[0].GetType();
    object instance = Activator.CreateInstance(requiredType);
    PropertyInfo[] pis = requiredType.GetProperties();
    foreach (var p in pis)
    {
        if (p.PropertyType == typeof(string))
        {
            p.SetValue(instance, string.Empty);
        }
    }
    

    请注意,如果类型没有无参数构造函数,则
    Activator.CreateInstance
    会引发异常。

    调用
    Activator.CreateInstance
    以创建新实例。然后使用
    PropertyInfo.SetValue
    将字符串字段设置为空

    Type requiredType = list[0].GetType();
    object instance = Activator.CreateInstance(requiredType);
    PropertyInfo[] pis = requiredType.GetProperties();
    foreach (var p in pis)
    {
        if (p.PropertyType == typeof(string))
        {
            p.SetValue(instance, string.Empty);
        }
    }
    

    请注意,如果类型没有无参数构造函数,则
    Activator.CreateInstance
    会引发异常。

    调用
    Activator.CreateInstance
    以创建新实例。然后使用
    PropertyInfo.SetValue
    将字符串字段设置为空

    Type requiredType = list[0].GetType();
    object instance = Activator.CreateInstance(requiredType);
    PropertyInfo[] pis = requiredType.GetProperties();
    foreach (var p in pis)
    {
        if (p.PropertyType == typeof(string))
        {
            p.SetValue(instance, string.Empty);
        }
    }
    

    请注意,如果类型没有无参数构造函数,则
    Activator.CreateInstance
    会引发异常。

    我不确定是否理解您的问题。如果使用反射获取未知类型的属性,