在 c# 中动态允许/拒绝属性



我正在使用一个带有属性的View Model将数据作为JSON传递给java脚本。

public string Property1 { get; set; }  //deny for user1
public string Property2{ get; set; }   //denyfor user2
public string Property3{ get; set; }    

现在我有不同类型的用户,因此根据用户,我希望拒绝或允许在UI上映射和显示属性

检查授权规则并在授权失败时返回 null 的自定义 getter 怎么样? 然后,可以将序列化程序设置为不序列化 null。

JsonSerializer serializer = new JsonSerializer { NullValueHandling = NullValueHandling.Ignore };
private string _property1;
public string Property1 
{ 
    get 
    {
        if(getcurrentuser.isauthorizedforproperty1)
        {
            return _property1;
        }
        else 
        {
            return null;
        }
    }
    set
    {
        _property1 = value;
    }
}

相关内容

最新更新