C# 授权属性

C# 授权属性,c#,wpf,c#-4.0,custom-attributes,C#,Wpf,C# 4.0,Custom Attributes,我有一个来自客户机的WPF应用程序,我需要为类中的某些方法添加一个ISINroleArtribute 所以我创建了这样的IsInRole类作为测试 namespace CustomAttributeSample { [System.AttributeUsage(System.AttributeTargets.Method)] public class IsInRoleAttribute : Attribute { public string Role {

我有一个来自客户机的WPF应用程序,我需要为类中的某些方法添加一个ISINroleArtribute

所以我创建了这样的IsInRole类作为测试

namespace CustomAttributeSample
{
    [System.AttributeUsage(System.AttributeTargets.Method)]
    public class IsInRoleAttribute : Attribute
    {
        public string Role { get; set; }

        public IsInRoleAttribute()
        {
            //open the xml file
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.Load("Data.xml");
        }

        public override bool Match(object obj)
        {
            return false;
        }

        public override bool Equals (object obj)
        {
            return false;
        }
    }
}
我把我的方法装饰成这样,再次作为迄今为止的一个测试

    [IsInRole(Role="Admin")]
    private bool Save()
    {
但是,我无法阻止执行该方法


毫无疑问,我可以在MVC中实现这一点,但这是我多年来的第一个WPF应用程序,因此我知道我在这里遗漏了一些东西。

对此也不太熟悉,但请尝试将其改为:

[AuthorizationAttribute(AuthorizationType.Allow, "Admin")]
如果您使用的是MVVM范例,那么这应该可以工作;-)


资料来源:

对此也不太熟悉,但请尝试将其改为:

[AuthorizationAttribute(AuthorizationType.Allow, "Admin")]
如果您使用的是MVVM范例,那么这应该可以工作;-)


来源:

谢谢。将在明天早上实施,并让您知道。下载了演示代码,它可以正常工作。现在了解如何在我的应用程序中使用它。:)谢谢将在明天早上实施,并让您知道。下载了演示代码,它可以正常工作。现在了解如何在我的应用程序中使用它。:)