在结构类型上找到静态方法后如何调用 [英] How to call static method once you found it on struct type
本文介绍了在结构类型上找到静态方法后如何调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个具有静态属性的结构(我不创建该结构的实例,而是像CommUser.MyProperty这样使用它).
I have a struct with static properties (I don't make instances of that struct, I am using it like a type - CommUser.MyProperty).
我已经编写了一种通过名称查找属性的方法.一旦找到该属性,我不知道该如何称呼它?像这样: CommUser.item
(找到项目属性).
I've written a method for finding property by name. What I don't know is how to call that property, once it's found? Something like: CommUser.item
(item is found property).
public struct CommUser
{
public static string pcUSER_URI_R97
{
get;
set;
}
public static string pcUSER_URI_R98
{
get;
set;
}
}
public bool CheckIfUserHasRights(string[] listUserRights)
{
var listUserProperties = typeof(CommUser).GetProperties(BindingFlags.Static | BindingFlags.Public);
foreach (var item in listUserProperties)
{
foreach (var usrRight in listUserRights)
{
if (item != null && !string.IsNullOrEmpty(item.Name) && !string.IsNullOrEmpty(usrRight))
{
if (item.Name.EndsWith(usrRight))
{
//how to make a call to CommUser.item ?
}
}
}
}
return false;
}
推荐答案
您使用 item.GetValue(null)
.该参数是实例,但是显然没有静态属性.
You use item.GetValue(null)
. The argument is the instance, but you don't have one for static properties, obviously.
这篇关于在结构类型上找到静态方法后如何调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文