2 回答
TA贡献1111条经验 获得超0个赞
您可以使用反射来做到这一点,如下所示:
public static class PermissionsExtensions {
public static T CommandGroup<T>(this Permissions permissions, string commandGroup)
{
PropertyInfo commandGroupProperty = typeof(Permissions).GetProperty(commandGroup);
return (T)(commandGroupProperty.GetValue( permissions));
}
public static bool CommandProperty<T>(this T commandGroup, string commandProperty)
{
PropertyInfo commandPropertyProperty = typeof(T).GetProperty( commandProperty);
return (bool)(commandPropertyProperty.GetValue( commandGroup));
}
}
然后你会像这样使用它:
bool result = rootObject.permissions.CommandGroup<Response>( "response").CommandProperty( "ping");
提示:类中的属性使用大写名称,参数使用小写名称
TA贡献1818条经验 获得超8个赞
看来你要访问的属性都是bool。您可以将它们全部存储在Dictionary<string, bool>:
internal class RootObject
{
public Dictionary<string, bool> permissions { get; set; } = new Dictionary<string, bool> {
{ "response.ping", false },
{ "response.helloworld", false },
// add more here...
};
public int points { get; set; }
}
现在你可以像这样访问字典:
if (rootObject.permissions[$"{commandGroup}.{commandName}"])
- 2 回答
- 0 关注
- 123 浏览
添加回答
举报