简体   繁体   English

如何使用反射将 PropertyInfo 转换为自己的类型

[英]How to cast PropertyInfo to its own type using reflection

I am trying to use reflection for getting the property name declared and its value, I am able to get the declared property name using property info the main concern I am having is I want to get the value for the property and I don't know the object type so I cant cast directly.我正在尝试使用反射来获取声明的属性名称及其值,我能够使用属性信息获取声明的属性名称我主要关心的是我想获取属性的值但我不知道object 类型所以我不能直接投射。

I know we need to use item.GetValue(object) but here the object, I need to pass using reflection.我知道我们需要使用 item.GetValue(object) 但这里是 object,我需要使用反射传递。

For example, if you see the below code例如,如果您看到以下代码

Class structure Class结构

public abstract class ObjectInputs{}

public class ValveInputs : ObjectInputs
{
    public Conditions Conditions { get; set; } = new Conditions();
}

public class Conditions :IExportable
{

    [CanExportAttribute]
    public string north {get;set;}    
}

Method方法

public void Append(Scenario scenario)
{
    var scenarioInputs = (commonDomain.ObjectInputs)scenario.Inputs; //  ObjectInputs is an abstract class

    var exportableInputs = scenarioInputs.GetType().GetProperties().Where(x =\> typeof(IExportable).IsAssignableFrom(x.PropertyType)); // I extraced property having interface IExportable

    var listOfExportableProperties = new ScenarioExtract();

    foreach (var exportableInput in exportableInputs)
    {
        var allProperties = ((System.Reflection.TypeInfo)exportableInput.PropertyType).DeclaredProperties; // Got all the property details

        var propertyHavingAttribute = allProperties.Where(x =\> x.CustomAttributes.Where(z =\> z.AttributeType == typeof(CanExportAttribute)).Any()).ToArray(); // Got the properties which i need to extract.

The issue is here, if i do this then its creating a new instance and the values of each properties are set to default.问题就在这里,如果我这样做,那么它会创建一个新实例,并且每个属性的值都设置为默认值。 I want to cast the exportableInput to its type (I cant hard code the type casting) so that i can use the value below.我想将 exportableInput 转换为其类型(我无法对类型转换进行硬编码)以便我可以使用下面的值。

        object destination = Activator.CreateInstance(scenarioInputs.GetType()); 

        foreach (var item in propertyHavingAttribute)
        {
            var detail = new InputPropertyDetail { InputName = item.Name, InputValue = \*\*item.GetValue(destination).ToString() \*\*};  \*\*want to use value here\*\*

            listOfExportableProperties.PropertyDetails.Add(detail);
        }
    }

    spreadsheetBuilder.AppendComponenet(listOfExportableProperties);
}

If you're using Activator.CreateInstance, it will always create a new instance (as the name inplies) with default values.如果您使用 Activator.CreateInstance,它将始终创建一个具有默认值的新实例(如名称所示)。 Instead you must use the value of the exportableInput property.相反,您必须使用exportableInput属性的值。

object destination = exportableInput.GetValue(scenarioInputs);

Then you can get the actual value of the exportable property of the instance with InputValue = item.GetValue(destination).ToString() .然后,您可以使用InputValue = item.GetValue(destination).ToString()实例的可导出属性的实际值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM