C# 如何清除动态对象内的值?
我正在将数据集转换为动态集合并绑定它,这很好。现在,当我需要向集合添加一个空的新对象时。我尝试的是获取datagrid的ItemsSource并获取列表中的第一个对象。但是它里面有一些价值。如何删除值并使用反射绑定空对象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
// 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
,对其进行迭代
调用
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
会引发异常。我不确定是否理解您的问题。如果使用反射获取未知类型的属性,