C# 处理自定义属性
我现在有一个自定义属性,我不知道如何限制对应用属性的方法的访问 例如:如果CustomRole的值是C# 处理自定义属性,c#,asp.net,silverlight,authorization,C#,Asp.net,Silverlight,Authorization,我现在有一个自定义属性,我不知道如何限制对应用属性的方法的访问 例如:如果CustomRole的值是“Admin”,则我现在有一个自定义属性,称为“CustomRole”,只有这样它才能访问方法 CustomRole["Admin"] public void Method() { // code } 如何执行验证值?这里需要某种方法。属性本身无法“访问”该方法,因为它不是由运行时计算的,但是您可以使用一些框架来拦截调用、检查属性和上下文并正确处理该情况 简而言之,你将: 用属性修饰
“Admin”
,则我现在有一个自定义属性,称为“CustomRole”
,只有这样它才能访问方法
CustomRole["Admin"]
public void Method()
{
// code
}
如何执行验证值?这里需要某种方法。属性本身无法“访问”该方法,因为它不是由运行时计算的,但是您可以使用一些框架来拦截调用、检查属性和上下文并正确处理该情况
简而言之,你将:
- 用属性修饰方法
- 提供一个拦截器来处理调用
- 通过一些提供AOP功能的工具实例化类
- 执行调用。呼叫将被拦截并根据您的实现进行处理
[RequiredPermission(Permissions.CanCreateOrder)]
public virtual Guid CreateOrder(string orderCode) {...}
拦截呼叫
现在您需要选择一个工具来实例化您的对象并拦截对它的调用。这可以通过支持AOP的IoC容器完成,也可以手动包装(例如,使用AOP工具创建对象的代理并使用代理)
您需要编写一个拦截器或包装器方法,在将执行转发给您的方法或拒绝调用之前,该方法有机会评估调用上下文
你可以。看看通过属性声明需求的OrderManagementService
类
穷人的AOP
您可以在不使用适当的AOP工具的情况下完成所有这些工作,但是可以使用某种形式的装饰器模式,以一种不太通用的方式(这对于更简单的项目来说可能非常好)——请注意,这是从head编写的,而不是在IDE中:
interface IService
{
void Method();
}
class ServiceImpl : IService // one of many implementations
{
[CustomRole("Admin")]
public void Method() { ... }
}
class ServiceChecker : IService // one of many implementations
{
IService m_svc;
public ServiceChecker(IService wrapped) { m_svc = wrapped; }
public void Method()
{
var mi = m_svc.GetType().GetMethod("Method");
if(mi.IsDefined(typeof(CustomRoleAttribute), true)
{
CustomRoleAttribute attr = (CustomRoleAttribute)mi.GetCustomAttributes(typeof(CustomRoleAttribute), true)[0];
if(!attr.Role.Equals( GetCurrentUserRole() ) // depends on where you get user data from
{
throw new SecurityException("Access denied");
}
}
m_svc.Method();
}
}
// the client code
IService svc = new ServiceChecker(new ServiceImpl());
svc.Method();
你需要某种方法。属性本身无法“访问”该方法,因为它不是由运行时计算的,但是您可以使用一些框架来拦截调用、检查属性和上下文并正确处理该情况
简而言之,你将:
- 用属性修饰方法
- 提供一个拦截器来处理调用
- 通过一些提供AOP功能的工具实例化类
- 执行调用。呼叫将被拦截并根据您的实现进行处理
[RequiredPermission(Permissions.CanCreateOrder)]
public virtual Guid CreateOrder(string orderCode) {...}
拦截呼叫
现在您需要选择一个工具来实例化您的对象并拦截对它的调用。这可以通过支持AOP的IoC容器完成,也可以手动包装(例如,使用AOP工具创建对象的代理并使用代理)
您需要编写一个拦截器或包装器方法,在将执行转发给您的方法或拒绝调用之前,该方法有机会评估调用上下文
你可以。看看通过属性声明需求的OrderManagementService
类
穷人的AOP
您可以在不使用适当的AOP工具的情况下完成所有这些工作,但是可以使用某种形式的装饰器模式,以一种不太通用的方式(这对于更简单的项目来说可能非常好)——请注意,这是从head编写的,而不是在IDE中:
interface IService
{
void Method();
}
class ServiceImpl : IService // one of many implementations
{
[CustomRole("Admin")]
public void Method() { ... }
}
class ServiceChecker : IService // one of many implementations
{
IService m_svc;
public ServiceChecker(IService wrapped) { m_svc = wrapped; }
public void Method()
{
var mi = m_svc.GetType().GetMethod("Method");
if(mi.IsDefined(typeof(CustomRoleAttribute), true)
{
CustomRoleAttribute attr = (CustomRoleAttribute)mi.GetCustomAttributes(typeof(CustomRoleAttribute), true)[0];
if(!attr.Role.Equals( GetCurrentUserRole() ) // depends on where you get user data from
{
throw new SecurityException("Access denied");
}
}
m_svc.Method();
}
}
// the client code
IService svc = new ServiceChecker(new ServiceImpl());
svc.Method();
你的代码看起来有点错误。 这是我使用
CustomRoleAttribute
public class MyClass
{
[CustomRole("Admin")]
public void MyMethod()
{
}
}
您的属性应该定义AttributeUsage
,以确保其他开发人员不会在属性或构造函数上使用您的属性:
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false,Inherited = true)]
public class CustomRoleAttribute : Attribute
{
public string Role { get; private set; }
public CustomRoleAttribute(string role)
{
Role = role;
}
}
现在把两者放在一起:
MyClass myClass = new MyClass();
MethodInfo[] methods = myClass.GetType().GetMethods(); // Access all the public methods.
foreach (var methodInfo in methods) // iterate trough all methods.
{
// Get all custom attributes from the method.
object[] attributes = methodInfo.GetCustomAttributes(typeof (CustomRoleAttribute), true);
if (attributes.Length > 0)
{
CustomRoleAttribute attribute = (CustomRoleAttribute)attributes[0];
if (attribute.Role == "Admin")
{
// the role is admin
}
}
}
现在,您可以看到如何使用属性。Bu您必须首先检查属性,然后访问方法 您的代码看起来有点错误。 这是我使用
CustomRoleAttribute
public class MyClass
{
[CustomRole("Admin")]
public void MyMethod()
{
}
}
您的属性应该定义AttributeUsage
,以确保其他开发人员不会在属性或构造函数上使用您的属性:
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false,Inherited = true)]
public class CustomRoleAttribute : Attribute
{
public string Role { get; private set; }
public CustomRoleAttribute(string role)
{
Role = role;
}
}
现在把两者放在一起:
MyClass myClass = new MyClass();
MethodInfo[] methods = myClass.GetType().GetMethods(); // Access all the public methods.
foreach (var methodInfo in methods) // iterate trough all methods.
{
// Get all custom attributes from the method.
object[] attributes = methodInfo.GetCustomAttributes(typeof (CustomRoleAttribute), true);
if (attributes.Length > 0)
{
CustomRoleAttribute attribute = (CustomRoleAttribute)attributes[0];
if (attribute.Role == "Admin")
{
// the role is admin
}
}
}
现在,您可以看到如何使用属性。Bu您必须首先检查属性,然后访问方法