反思、收藏、词典 天哪!

发布于 2024-10-04 03:17:57 字数 1317 浏览 2 评论 0原文

我在使用反射和访问集合时遇到了一些问题:

XmlElement xmlObject = Scene.CreateElement("Object");
Type Target = obj.GetType();

... xml code here

PropertyInfo[] props = Target.GetProperties(BindingFlags.Public|BindingFlags.NonPublic|BindingFlags.Instance|BindingFlags.Static);

foreach (PropertyInfo prop in props)
{
 Type propType = prop.PropertyType;

 if ((propType.IsPublic && propType.IsValueType && prop.CanRead && prop.CanWrite)
  || PropertyNameExceptions.Contains(prop.Name)
  || PropertyTypeExceptions.Contains(prop.PropertyType.Name))
 {

  object result = null;
  try
  {
   result = prop.GetValue(obj, null);
  }
  catch
  {

  }
  }

 else if (isCollection(result))
 {

  Type pType = result.GetType();
  PropertyInfo[] pTypeInfo = pType.GetProperties();

  ICollection<object> rCollection = null;
  try
  {
   rCollection = (ICollection<object>)prop.GetValue(obj, null);
  }
  catch
  {

  }

  foreach (object o in rCollection)
  {
   ObjectToXML(o, xmlPropertyObject);
  }
 }   
}

private bool isCollection(object o)
{
 if (o.GetType().GetInterface("ICollection") != null)
 {
 return true;
 }

 return false;
}

无法将类型为 'ValueCollection[System.String,Axiom.Core.MovableObject]' 的对象转换为类型 'System.Collections.Generic.ICollection`1[System.Object ]'。

I am having a bit of a problem using reflection and accessing collections:

XmlElement xmlObject = Scene.CreateElement("Object");
Type Target = obj.GetType();

... xml code here

PropertyInfo[] props = Target.GetProperties(BindingFlags.Public|BindingFlags.NonPublic|BindingFlags.Instance|BindingFlags.Static);

foreach (PropertyInfo prop in props)
{
 Type propType = prop.PropertyType;

 if ((propType.IsPublic && propType.IsValueType && prop.CanRead && prop.CanWrite)
  || PropertyNameExceptions.Contains(prop.Name)
  || PropertyTypeExceptions.Contains(prop.PropertyType.Name))
 {

  object result = null;
  try
  {
   result = prop.GetValue(obj, null);
  }
  catch
  {

  }
  }

 else if (isCollection(result))
 {

  Type pType = result.GetType();
  PropertyInfo[] pTypeInfo = pType.GetProperties();

  ICollection<object> rCollection = null;
  try
  {
   rCollection = (ICollection<object>)prop.GetValue(obj, null);
  }
  catch
  {

  }

  foreach (object o in rCollection)
  {
   ObjectToXML(o, xmlPropertyObject);
  }
 }   
}

private bool isCollection(object o)
{
 if (o.GetType().GetInterface("ICollection") != null)
 {
 return true;
 }

 return false;
}

Unable to cast object of type 'ValueCollection[System.String,Axiom.Core.MovableObject]' to type 'System.Collections.Generic.ICollection`1[System.Object]'.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧人哭 2024-10-11 03:17:57

您测试 ICollection 的非通用版本是否由对象实现,并乐意尝试将其强制转换为 ICollection...

要么测试该对象是否真正实现 < code>ICollection

private bool isCollection(object o)
{
    return o is ICollection<object>;
}

或使用类似的东西

rCollection = ((IEnumerable)prop.GetValue(obj, null)).OfType<Object>().ToList();

You test if the non generic version of ICollection is implementend by an object and hapilly try to cast it to ICollection<Object> ...

Either test if the object really implement ICollection<Object> :

private bool isCollection(object o)
{
    return o is ICollection<object>;
}

or use something like

rCollection = ((IEnumerable)prop.GetValue(obj, null)).OfType<Object>().ToList();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文