简体   繁体   English

使用反射获取对象内部的属性值不起作用

[英]Getting property values inside object using reflection doesn't work

Trying to get into reflection and as a noob I don't understand why this isn't working.试图进行反思,作为一个菜鸟,我不明白为什么这不起作用。

foreach (var p in localStorageProfile.GetType().GetProperties())
{
    userprofile.Name = p.GetValue("name").ToString();
    userprofile.PhotoUrl = p.GetValue("photoUrl").ToString();
}

I used ToString because they should be strings我使用ToString因为它们应该是字符串

This is the exception这是例外

System.Reflection.TargetException: Object does not match target type. at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)

And the object as it comes from the client looks like this:来自客户端的对象如下所示:

{{"id":0,"name":"Mollie","photoUrl":"assets/images/portraits/1.png","imagePublicId":null... I wanna get properties inside the object type this way because I wanna get into reflection a little bit to get used to it. {{"id":0,"name":"Mollie","photoUrl":"assets/images/portraits/1.png","imagePublicId":null...我想以这种方式获取object类型中的属性因为我想稍微反思一下以适应它。 And this code should work这段代码应该可以工作

I would assume that in the example you have provided the variable localStorageProfile is of type string and contains the JSON data.我假设在示例中您提供的变量localStorageProfilestring类型并包含 JSON 数据。

This would mean that localStorageProfile.GetType().GetProperties() is equivalent to writing out typeof(System.String).GetProperties() which would ultimately return properties of the System.String class and not JSON values.这意味着localStorageProfile.GetType().GetProperties()等效于写出typeof(System.String).GetProperties() ,最终将返回System.String类的属性而不是 JSON 值。

Usually you want to use the reflection methods GetProperty() and GetProperties() with class objects that actually contain useful property information.通常您希望对实际包含有用属性信息的类对象使用反射方法GetProperty()GetProperties()

An primitive example could be:一个原始的例子可能是:

public class Profile
{
    public Profile() { }

    public Profile(int id, string name, string photoUrl)
    {
        this.Id = id;
        this.Name = name;
        this.PhotoUrl = photoUrl;
    }

    public int Id { get; set; }
    public string Name { get; set; }
    public string PhotoUrl { get; set; }
}

// Creating a sample profile
var dollie = new Profile(1, "Dollie", "assets/images/portraits/2.png");

// Creating an empty profile
var copyOfDollie = new Profile();

// Filling the empty profile using reflection
copyOfDollie.Id = (int)typeof(Profile).GetProperty("Id").GetValue(dollie);
copyOfDollie.Name = (string)typeof(Profile).GetProperty("Name").GetValue(dollie);
copyOfDollie.PhotoUrl = (string)typeof(Profile).GetProperty("PhotoUrl").GetValue(dollie);

If you just intended to parse the JSON instead use the JsonSerializer class:如果您只想解析 JSON,请改用JsonSerializer类:

string json = @"{
                   ""Id""  : 0,
                   ""Name"": ""Mollie"",
                   ""PhotoUrl"": ""assets/images/portraits/1.png""
                }";

var mollie = JsonSerializer.Deserialize<Profile>(json);

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

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