代码之家  ›  专栏  ›  技术社区  ›  FrenkyB

在结构类型上找到静态方法后如何调用它

  •  1
  • FrenkyB  · 技术社区  · 6 年前

    我有一个具有静态属性的结构(我不创建该结构的实例,我使用它就像一个类型-commuser.myproperty)。

    我写了一个按名称查找属性的方法。我不知道的是,一旦找到那块地产怎么称呼它?比如: CommUser.item (项已找到属性)。

    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;
    }
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Joey Gumbo    6 年前

    你用 item.GetValue(null) . 参数是实例,但显然静态属性没有。