应用框架的设计与实现——.NET平台(10 授权服务.Attribute)

授权服务.Authorization Service

.net 框架为我们提供了 System.Security.Permissions.SecurityAttribute 特性类,
该类支持通过为类中的方法标注特性标签的方式,来实施按角色划分级别的安全授权方案。
为了使业务组件的访问权限声明从业务组件本身分离出来,模块设计使用了.net特性(.net attribute)
和.net声明性安全权限(.net declarative security permission)模型这两项技术。


.net 特性
为类和方法标注特性只是提供了类和方法的相关信息,至于如何使用这些信息,则取决于类和方法的用户。
.net框架提供了一些预定义特性,.net编译器和CLR通常会使用这些预定义特性中的信息来执行某些操作。

原生特性对象(native attribute object) ?

自定义特性需继承自Attribute或其子类。Nunit的测试属性类:

    [AttributeUsage(AttributeTargets.Class, AllowMultiple = false , Inherited = true )]
    
public   sealed   class  TestFixtureAttribute : Attribute
    
{
        
private string description;

        
/// <summary>
        
/// Descriptive text for this fixture
        
/// </summary>

        public string Description
        
{
            
get return description; }
            
set { description = value; }
        }

    }



在代码中使用自定义的属性:

  
 [TestFixture]
    [Obsolete(
" use TestFixture attribute instead of inheritance " , false )]
    
public   class  TestCase : Assertion
    
{
        
/// <summary>
        
/// SetUp method
        
/// </summary>

        [SetUp]
        [Obsolete(
"use SetUp attribute instead of naming convention",false)]
        
protected virtual void SetUp()
        
{}

        
/// <summary>
        
/// TearDown method
        
/// </summary>

        [TearDown]
        [Obsolete(
"use TearDown attribute instead of naming convention",false)]
        
protected virtual void TearDown()
        
{}
    }



已知一个程序中使用了某属性,我们如何处理这个程序,如何使用属性提供的信息:

         private   static   string  GetDescription(MethodInfo method)
        
{
            Type testAttr 
= typeof(NUnit.Framework.TestAttribute);
            
object[] attributes = method.GetCustomAttributes(testAttr, false);

            
string description = null;

            
if(attributes.Length == 1)
            
{
                NUnit.Framework.TestAttribute attribute 
= (NUnit.Framework.TestAttribute)attributes[0];
                description 
= attribute.Description;
            }


            
return description;
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值