Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.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# 如何将System.Reflection.PropertyInfo对象转换为其原始对象类型_C#_C# 4.0 - Fatal编程技术网

C# 如何将System.Reflection.PropertyInfo对象转换为其原始对象类型

C# 如何将System.Reflection.PropertyInfo对象转换为其原始对象类型,c#,c#-4.0,C#,C# 4.0,嗯,正在寻找将System.Reflection.PropertyInfo转换为其原始对象的方法 public static Child ConvertToChiildObject(this PropertyInfo propertyInfo) { var p = (Child)propertyInfo; } propertyInfo对象actulayy包含这样一个类 public class Child{ public string name = "S

嗯,正在寻找将System.Reflection.PropertyInfo转换为其原始对象的方法

 public static Child ConvertToChiildObject(this PropertyInfo propertyInfo)
    {
        var p = (Child)propertyInfo;

    }
propertyInfo对象actulayy包含这样一个类

public class Child{
  public string name = "S";
  public string age = "44";

}
到目前为止,我已经尝试过隐式铸造 有办法吗?

试试这个:

public static Child ConvertToChildObject(this PropertyInfo propertyInfo, object parent)
{
    var source = propertyInfo.GetValue(parent, null);
    var destination = Activator.CreateInstance(propertyInfo.PropertyType);

    foreach (PropertyInfo prop in destination.GetType().GetProperties().ToList())
    {
       var value = source.GetType().GetProperty(prop.Name).GetValue(source, null);
       prop.SetValue(destination, value, null);
    }

    return (Child) destination; 
}

在上面,我使用了额外的参数
parent
,它是
子对象的基本对象

我必须说明,这不是问题的答案,而是教育练习

正如其他人所解释的,您误解了
PropertyInfo
类的用法。此类用于描述属性,不包含与实例相关的数据。因此,如果不提供一些额外的信息,您将无法尝试执行

现在,
PropertyInfo
类可以从对象中获取与实例相关的数据,但是必须有对象的实例才能从中读取数据

例如,以下面的类结构为例

public class Child
{
    public string name = "S";
    public string age = "44";
}

public class Parent
{
    public Parent()
    {
        Child = new Child();
    }

    public Child Child { get; set; }
}
属性
子类
父类
的属性。构建父类时,将创建一个新的
实例作为
父实例的一部分

然后,我们可以使用
Reflection
通过简单调用来获取属性
子属性的值

var parent = new Parent();
var childProp = typeof(Parent).GetProperty("Child");
var childValue = (Child)childProp.GetValue(parent);
这个很好用。重要的部分是
(Child)childProp.GetValue(parent)
。请注意,我们正在访问
PropertyInfo
类的
GetValue(object)
方法,以从
父类的实例中检索
子属性的值

这是您必须设计访问属性数据的方法的基本方式。但是,正如我们多次列出的,您必须拥有该属性的实例。现在我们可以编写一个扩展方法来简化这个调用。在我看来,使用扩展方法没有任何好处,因为现有的
PropertyInfo.GetValue(object)
方法使用起来非常快。但是,如果您希望创建父对象的新实例,然后获取该值,那么您可以编写一个非常简单的扩展方法

public static TPropertyType ConvertToChildObject<TInstanceType, TPropertyType>(this PropertyInfo propertyInfo, TInstanceType instance)
    where TInstanceType : class, new()
{
    if (instance == null)
        instance = Activator.CreateInstance<TInstanceType>();

    //var p = (Child)propertyInfo;
    return (TPropertyType)propertyInfo.GetValue(instance);

}
请注意拥有一个实例的重要性

一个警告:如果对象为null,扩展方法将通过调用:

if (instance == null)
    instance = Activator.CreateInstance<TInstanceType>();
if(实例==null)
instance=Activator.CreateInstance();
您还将注意到
typeparam
TInstanceType
必须是
,并且必须确认
new()
限制。这意味着它必须是
,并且必须具有无参数构造函数


我知道这不是问题的解决方案,但希望能有所帮助。

我不清楚这个问题。是否要获取
属性info
的值?否则?您将需要执行
(子)propertyInfo.GetValue(obj)
——但您确实需要具有该属性的对象实例property@Irshad我已经更新了我的question@Rob我已经更新了我的问题,所需的对象将作为PropertyInfo出现,因为您误解了
PropertyInfo
的工作原理。它描述的是一个
属性
,但要读取该值,还必须有一个实例。例如,
父类
类可能有一个属性
公共子类{get;set;}
propertyInfo
将描述
Parent.child
,但不描述值本身。此代码根本不起作用“destination.GetProperties()”它应该是destination.GetTyp().GetProperties()。我已经修复了这些,非常感谢您。非常感谢您,我非常感谢您的回答,这对我帮助很大!
childValue = name: S, age: 44
childValueNull = name: S, age: 100
if (instance == null)
    instance = Activator.CreateInstance<TInstanceType>();