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 關注
- 158 瀏覽
添加回答
舉報