浅谈Attribute [C# | Attribute | DefaultValueAttribute]
前言
最近做权限控制,对页面的权限控制使用IHttpModule做的,想把控制粒度细化到页面上控件的权限判断,意图是传入控件编号,根据控件编号和当前用户的权限,如果没有权限的话就把控件隐藏或显示为不可用,打算用Attribute来做,以下是遇到的一些问题和看法,虽然失败了但是觉得仍然有参考价值。
推荐几篇文章:
正文:
先看我的错误代码:
public class PowerAttribute : Attribute
{
/// <summary>
/// 对页面内控件进行权限判断,权限的字符串组成:URL+"?id"+Control.ID
/// 例如:/page/index?id=tbName
/// </summary>
/// <param name="pType"></param>
/// <param name="control"></param>
public PowerAttribute(PowerType pType, params WebControl[] control)
{
for (int i = control.Length; --i >= 0; )
{
WebControl Control = control.GetValue(i) as WebControl;
if (Control != null)
if (HasPower(string.Concat(WebHelper.WebRequest.Path, "?id=", Control.ID)) == -1)//判断权限
if (pType == PowerType.Visible)
Control.Visible = false;
else
Control.Enabled = false;
}
}
public int HasPower(string path)
{
return -1;
}
/// <summary>
/// 获得控件的权限,1 具有权限,0 用户超时,-1 没有权限
/// </summary>
/// <param name="control"></param>
/// <param name="Field"></param>
public PowerAttribute(WebControl control, ref int Field)
{
Field = HasPower(string.Concat(WebHelper.WebRequest.Path, "?id=", control.ID));
}
/// <summary>
/// 获得页面的权限,1 具有权限,0 用户超时,-1 没有权限
/// </summary>
/// <param name="control"></param>
/// <param name="Field"></param>
public PowerAttribute(ref int Field)
{
Field = HasPower(WebHelper.WebRequest.Path);
}
}
意图比较明显,想在要控制控件权限的页面里面加一个Attribute标记传入控件编号就能自动把其设置为指定的显示状态,可是失败了!以下是错误信息:
也就是说没法传,也不能够用ref来传递引用!也就是说没有办法直接改变页面上的控件或字段的值,这让我忽然想起了DefaultValueAttribute,MSDN的解释是
指定属性 (Property) 的默认值。我发现有许多朋友有点误会这个,先看代码:
{
AttributeCollection attributes = TypeDescriptor.GetProperties(new MyClass())["MyProperty"].Attributes;
/* Prints the default value by retrieving the DefaultValueAttribute
* from the AttributeCollection. */
DefaultValueAttribute myAttribute =
(DefaultValueAttribute)attributes[typeof(DefaultValueAttribute)];
Console.WriteLine("The default value is: " + myAttribute.Value.ToString());
Console.Read();
}
class MyClass
{
private bool myVal = false;
[DefaultValue(false)]
public bool MyProperty
{
get
{
return myVal;
}
set
{
myVal = value;
}
}
}
这是MSDN例子,注意看两点:
1. DefaultValue传入参数为false,而myVal设置的也是false
2. 再看调用的地方,是使用的DefaultValueAttribute,而不是直接访问的属性MyProperty
狐疑了吧,他设置的默认值不是属性MyProperty的默认值!!不信的话你可以把[DefaultValue(false)]改成[DefaultValue(true)],然后直接Console.WriteLine(new MyClass().MyProperty);!!
从上面参考的几篇文章中可以发现,Attribute可以做方法参数校验、也可以为过时的方法做标记 ObsoleteAttribute、为方法传参,虽然如此,但是仍然觉的使用起来并不方便,只是看起来比较酷罢了,一个类居然可以这样使用!!可以把这个类仅仅使用在类、方法、字段上,也可以使用所以地方,控制使用次数,显然仍然是有他的用处的,没法把你的代码和他耦合起来,也是用反射来实现相关功能的!!
说到这里实在还是不太明白,但是起码知道DefaultValueAttribute不是为当前类设置默认值的,所以使用的时候注意Attribute概念问题!欢迎大家提意见: )
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库