C#表达式树是任务的正确工具吗?
我的示例项目是一个MVC WebApi项目 标准authorized属性采用Roles=“”或Users=“”参数。 我还没有看过实现,但我也不想:) 你能用表达式来证明这一点吗? 在某种程度上,当您决定将属性的“角色”属性重命名为其他属性时,会出现编译时错误C#表达式树是任务的正确工具吗?,c#,lambda,expression-trees,C#,Lambda,Expression Trees,我的示例项目是一个MVC WebApi项目 标准authorized属性采用Roles=“”或Users=“”参数。 我还没有看过实现,但我也不想:) 你能用表达式来证明这一点吗? 在某种程度上,当您决定将属性的“角色”属性重命名为其他属性时,会出现编译时错误 public interface IControllerContext { int MagicNumber { get; } int ScaryNumber { get; } string Version { ge
public interface IControllerContext
{
int MagicNumber { get; }
int ScaryNumber { get; }
string Version { get; }
}
public class AppAuthorizationAttribute : FilterAttribute
{
public IControllerContext SomeControllerContext
{
get; // implementation omitted
}
// ...
}
// This is my sample class which needs to be validated using the attribute.
public class TestClass : BaseClass
{
[AppAuthorization((n) => n.MagicNumber == 13)] // or literally: which property and how to validate it"
protected void SomeMethodWhichRequiresPreProcessingValidation()
{
// do something.
// inside here I have access to an instance of ISomeContext
// and can do anything I want.
}
}
可选的额外问题:是否可以从属性定义中访问当前控制器(非静态)的字段?将控制器字段注入验证lambda表达式将是一件很酷的事情。类似于:AppAuthorization((controller)=>controller.SomePropertyHere.MagicNumer==13)属性声明只能使用。编译器将不允许您使用表达式,因此您计划的方法将不起作用 也不能引用字段或属性,只能引用常量。你能得到的最接近的东西是
public const MagicNumberPropertyName = "MagicNumber";
public enum Operator
{
Equals
}
[AppAuthorization(MagicNumberPropertyName, Operator.Equals, 13)]
protected void Method() {}
属性声明只能使用。编译器将不允许您使用表达式,因此您计划的方法将不起作用 也不能引用字段或属性,只能引用常量。你能得到的最接近的东西是
public const MagicNumberPropertyName = "MagicNumber";
public enum Operator
{
Equals
}
[AppAuthorization(MagicNumberPropertyName, Operator.Equals, 13)]
protected void Method() {}
下面是一个示例,您可以使用Enum作为authorize属性,您可以在authorize中编写逻辑
/// <summary>
/// Class RoleAuthorizeAttribute.
/// </summary>
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = true, AllowMultiple = true)]
public class RoleAuthorizeAttribute : AuthorizeAttribute
{
/// <summary>
/// Initializes a new instance of the <see cref="RoleAuthorizeAttribute"/> class.
/// </summary>
/// <param name="roles">The roles.</param>
/// <exception cref="System.ArgumentException">The roles parameter may only contain enums;roles</exception>
public RoleAuthorizeAttribute(params object[] roles)
{
if (roles.Any(r => r.GetType().BaseType != typeof (Enum)))
{
throw new ArgumentException(
"The roles parameter may only contain enums",
"roles");
}
Roles = string.Join(",", roles.Select(r => Enum.GetName(r.GetType(), r)).ToList());
}
}
//
///类RoleAuthorizeAttribute。
///
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class,Inherited=true,AllowMultiple=true)]
公共类RoleAuthorizeAttribute:AuthorizeAttribute
{
///
///初始化类的新实例。
///
///角色。
///roles参数只能包含枚举;roles
public RoleAuthorizeAttribute(参数对象[]角色)
{
if(roles.Any(r=>r.GetType().BaseType!=typeof(Enum)))
{
抛出新的ArgumentException(
“角色参数只能包含枚举”,
“角色”);
}
Roles=string.Join(“,”,Roles.Select(r=>Enum.GetName(r.GetType(),r)).ToList();
}
}
下面是一个示例,您可以使用Enum作为authorize属性,您可以在authorize中写入逻辑
/// <summary>
/// Class RoleAuthorizeAttribute.
/// </summary>
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = true, AllowMultiple = true)]
public class RoleAuthorizeAttribute : AuthorizeAttribute
{
/// <summary>
/// Initializes a new instance of the <see cref="RoleAuthorizeAttribute"/> class.
/// </summary>
/// <param name="roles">The roles.</param>
/// <exception cref="System.ArgumentException">The roles parameter may only contain enums;roles</exception>
public RoleAuthorizeAttribute(params object[] roles)
{
if (roles.Any(r => r.GetType().BaseType != typeof (Enum)))
{
throw new ArgumentException(
"The roles parameter may only contain enums",
"roles");
}
Roles = string.Join(",", roles.Select(r => Enum.GetName(r.GetType(), r)).ToList());
}
}
//
///类RoleAuthorizeAttribute。
///
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class,Inherited=true,AllowMultiple=true)]
公共类RoleAuthorizeAttribute:AuthorizeAttribute
{
///
///初始化类的新实例。
///
///角色。
///roles参数只能包含枚举;roles
public RoleAuthorizeAttribute(参数对象[]角色)
{
if(roles.Any(r=>r.GetType().BaseType!=typeof(Enum)))
{
抛出新的ArgumentException(
“角色参数只能包含枚举”,
“角色”);
}
Roles=string.Join(“,”,Roles.Select(r=>Enum.GetName(r.GetType(),r)).ToList();
}
}
你真的认为AppAuthorization
是属性吗?不是在上面的伪代码中,而是假设它是属性。我将更新示例。不能将lambdas用作属性参数。对于有效属性参数类型的列表,check并不真正理解此处的特定场景,但超出了前面注释中提到的一些限制,您可能会添加额外的编译时验证。您真的认为AppAuthorization
是属性吗?不在上面的伪代码中,但假设是。我将更新示例。不能将lambdas用作属性参数。对于有效属性参数类型的列表,check并不真正理解这里的特定场景,但是除了前面注释中提到的一些限制之外,您还可以添加额外的编译时验证。