Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用POCO或普通C实体类的通用方法实现属性值分配控制_C#_Authorization - Fatal编程技术网

C# 使用POCO或普通C实体类的通用方法实现属性值分配控制

C# 使用POCO或普通C实体类的通用方法实现属性值分配控制,c#,authorization,C#,Authorization,我正在开发一个.NET应用程序&试图用C实现以下目标 让我们假设下面有一个实体作为Employee类,具有很少的公共自动实现属性。 假设我们已经创建了一个属性,比如BeforePropertySet&我们已经用它注释了一些实体属性,如下面的类中所示 class Employee { [BeforePropertySet("Some Delegate", "some other Prop value")] public string Address { get; set; }

我正在开发一个.NET应用程序&试图用C实现以下目标

让我们假设下面有一个实体作为Employee类,具有很少的公共自动实现属性。 假设我们已经创建了一个属性,比如BeforePropertySet&我们已经用它注释了一些实体属性,如下面的类中所示

class Employee
{
    [BeforePropertySet("Some Delegate", "some other Prop value")]
    public string Address { get; set; }

    public string Name { get; set; }

    public int age { get; set; }
}
我想做的事情可以通过代码片段来解释-

var e = new Employee();
e.Address = "confidential data";  
//this assignment is restricted for user X
//whereas allowed for user Y
e.Name = "general data";
每当我给一个属性赋值时,如果该属性在PreforePropertySet之前用这个特殊属性进行了注释,那么它将回调一个公共函数,该函数将在整个应用程序中执行一些标准操作。例如,如果不希望currentUser查看分配的值,则可以取消新的值分配

这种方法使我能够轻松地删除属性或添加到新实体

我找到了一种方法,它允许我调用另一个函数,而对继承类中的调用几乎没有更改,但它是一个自动生成的继承类&需要为此方法显式创建实例


有人试过这个吗?请让我知道如何实现这一点,或者请指出您可能知道的任何其他资源。谢谢你的帮助。非常感谢。

感谢给出可能答案的人。为了完整起见,请在此发布。下面是我为什么改变路径并仍然在应用程序中实现业务目标的原因和方式

与我的团队讨论后,取消了基于属性的方法。 主要原因是能够动态更改数据保护策略,而无需重新访问代码。 考虑到这一点,我最终做了以下工作

1在xml中定义元数据并将其缓存。 2所有模型转换/创建-分配在每个模型的单个类中进行。 3添加了一个helper方法,该方法检查xml中当前用户是否有权基于xml配置访问受保护的数据并分配适当的值

XML看起来像-

 <ModelTag Name="SomeModelName">
    <Property defaultReturnValue="0" roles="userrole1, userrole2">PropertyName1</Property>
  </ModelTag>

我希望这能帮助别人

您应该看看面向方面的编程
Employee e  = new Employee();
e.PropertyName1 = (CheckAccess(e,"PropertyName1") ? "new value2" : null);
e.PropertyName2 = (CheckAccess(e,"PropertyName1") ? "new value2" : null);


bool CheckAccess(model m, string propName)
{
    var currentRole = GetCurrentRole();  //some method to retrive Role of current user
    //Retrieve xml for model m from cache
    //Check if XML configuration provides access to values of propName to currentRole
    //Return true or false accordingly
}