Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.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# 从类动态获取属性值_C#_Winforms - Fatal编程技术网

C# 从类动态获取属性值

C# 从类动态获取属性值,c#,winforms,C#,Winforms,我有一个类,我想从中动态检索属性 这是课堂上的一个例子 namespace TEST { public class Data { public string Username { get; set; } public string Password { get; set; } } } 我试图使用GetProperty,但它总是返回null static object PropertyGet(object p, string propNam

我有一个类,我想从中动态检索属性

这是课堂上的一个例子

namespace TEST
{
    public class Data
    {
        public string Username { get; set; }
        public string Password { get; set; }
    }
}
我试图使用GetProperty,但它总是返回null

static object PropertyGet(object p, string propName)
{
    Type t = p.GetType();
    PropertyInfo info = t.GetProperty(propName);
    if (info == null)
        return null;
    return info.GetValue(propName);
}
像这样

    var data = new Data();

    var x = PropertyGet(data, "Username");
    Console.Write(x?? "NULL");

这行是错误的,应该为您抛出一个异常:

return info.GetValue(propName);
您需要传入要从中提取属性的对象,即

return info.GetValue(p);
还要注意,当前的
数据。用户名
为空。你想要的是:

var data = new Data { Username = "Fred" };

我已经验证了这两个更改是否有效。

这一行是错误的,应该为您抛出一个异常:

return info.GetValue(propName);
您需要传入要从中提取属性的对象,即

return info.GetValue(p);
还要注意,当前的
数据。用户名
为空。你想要的是:

var data = new Data { Username = "Fred" };
我已经验证了通过这两个更改,它是有效的。

这是有效的:

public class Data
{
    public string Username { get; set; }
    public string Password { get; set; }
}

public class Program
{
    static object PropertyGet(object p, string propName)
    {
        Type t = p.GetType();
        PropertyInfo info = t.GetProperty(propName);

        if (info == null)
        {
            return null;
        }
        else
        {
            return info.GetValue(p, null);
        }
    }

    static void Main(string[] args)
    {
        var data = new Data() { Username = "Fred" };

        var x = PropertyGet(data, "Username");
        Console.Write(x ?? "NULL");
    }
}
这项工作:

public class Data
{
    public string Username { get; set; }
    public string Password { get; set; }
}

public class Program
{
    static object PropertyGet(object p, string propName)
    {
        Type t = p.GetType();
        PropertyInfo info = t.GetProperty(propName);

        if (info == null)
        {
            return null;
        }
        else
        {
            return info.GetValue(p, null);
        }
    }

    static void Main(string[] args)
    {
        var data = new Data() { Username = "Fred" };

        var x = PropertyGet(data, "Username");
        Console.Write(x ?? "NULL");
    }
}

我试过了,但仍然是空的,而且如果我传递了p,这是类,我怎么能指定我需要的属性?!!好的,好的,它正在处理字符串,但是如果我已经订购了Dictionary并想从中获取一些值,该怎么办?@DanialEugen:您已经这样做了,因为
info
属性info
。如果对象的属性值为非空值,则不会得到空值。@DanialEugen:然后像以前一样获取属性值(“代码”>“OrderedDictionary”
),强制转换为正确的类型,然后执行任何您想要的操作。我试过了,但仍然有空值,另外,如果我通过了p,这是一个类,我怎么能指定我需要的属性?!!好的,好的,它正在处理字符串,但是如果我已经订购了Dictionary并想从中获取一些值,该怎么办?@DanialEugen:您已经这样做了,因为
info
属性info
。如果对象的属性值为非空值,则不会得到空值。@DanialEugen:然后像以前一样获取属性值(“代码>有序字典”
),转换为正确的类型,然后执行任何操作。