• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
皇图霸业谈笑间
更高、更快、更强
博客园    首页    新随笔    联系   管理    订阅  订阅
老赵一个有用文章摘录(Attribute)
代码
[AttributeUsage(AttributeTargets.All, AllowMultiple = true)]
public class AttachDataAttribute : Attribute
{
    
public AttachDataAttribute(object key, object value)
    {
        
this.Key = key;
        
this.Value = value;
    }

    
public object Key { get; private set; }

    
public object Value { get; private set; }
}

public static class AttachDataExtensions
{
    
public static object GetAttachedData(
        
this ICustomAttributeProvider provider, object key)
    {
        var attributes 
= (AttachDataAttribute[])provider.GetCustomAttributes(
            
typeof(AttachDataAttribute), false);
        
return attributes.First(a => a.Key.Equals(key)).Value;
    }

    
public static T GetAttachedData<T>(
        
this ICustomAttributeProvider provider, object key)
    {
        
return (T)provider.GetAttachedData(key);
    }

    
public static object GetAttachedData(this Enum value, object key)
    {
        
return value.GetType().GetField(value.ToString()).GetAttachedData(key);
    }

    
public static T GetAttachedData<T>(this Enum value, object key)
    {
        
return (T)value.GetAttachedData(key);
    }
}


 AttachDataAttribute是一个自定义属性,可以在各式成员上进行标记。它允许任何类型的Key和任何类型的Value,而开发人员就可以 使用GetAttachedData方法,通过一个Key来获得其对应的Value。这个方法比较简单,但是大量用于老赵的项目中。以下便是一例:

 

代码
public enum AgeRange
{ 
    [AttachData(AgeRangeAttachData.Text, 
"18岁及以下")]
    LessThan18,

    [AttachData(AgeRangeAttachData.Text, 
"19至29岁")]
    From19To29,

    [AttachData(AgeRangeAttachData.Text, 
"30岁及以上")]
    Above29
}

public enum AgeRangeAttachData
{ 
    Text
}

public static class AgeRangeExtensions
{
    
public static string GetText(this AgeRange range)
    {
        
return range.GetAttachedData<string>(AgeRangeAttachData.Text);
    }
}


 枚举的每一项其实相当于一个只读的共有Field,也可以加上自定义属性。我们为AgeRange枚举的每一项加上一个以 AgeRangeAttachData为Key的附加值作为该枚举的显示文字,并且针对AgeRange编写一个扩展方法GetText。于是代码中就可 以使用以下方式来获取某个枚举值的显示文字了:

 

AgeRange.From19To29.GetText()


一 个利用扩展方法的实例:AttachDataExtensions

2009-01-07 14:05 by Jeffrey Zhao, 15120 visits, 网摘, 收 藏, 编 辑

扩展方法是C# 3.0(老赵对VB不熟)中最简单,也是最常用的语言特性之一。这是老赵自以为的一个简单却不失经典的实例:

[AttributeUsage(AttributeTargets.All, AllowMultiple = true)]
public class AttachDataAttribute : Attribute
{
public AttachDataAttribute(object key, object value)
{
this.Key = key;
this.Value = value;
}

public object Key { get; private set; }

public object Value { get; private set; }
}

public static class AttachDataExtensions
{
public static object GetAttachedData(
this ICustomAttributeProvider provider, object key)
{
var attributes = (AttachDataAttribute[])provider.GetCustomAttributes(
typeof(AttachDataAttribute), false);
return attributes.First(a => a.Key.Equals(key)).Value;
}

public static T GetAttachedData<T>(
this ICustomAttributeProvider provider, object key)
{
return (T)provider.GetAttachedData(key);
}

public static object GetAttachedData(this Enum value, object key)
{
return value.GetType().GetField(value.ToString()).GetAttachedData(key);
}

public static T GetAttachedData<T>(this Enum value, object key)
{
return (T)value.GetAttachedData(key);
}
}

AttachDataAttribute是一个自定义属性,可以在各式成员上进行标记。它允许任何类型的Key和任何类型的Value,而开发人员 就可以使用GetAttachedData方法,通过一个Key来获得其对应的Value。这个方法比较简单,但是大量用于老赵的项目中。以下便是一例:

public enum AgeRange
{
[AttachData(AgeRangeAttachData.Text, "18岁及以下")]
LessThan18,

[AttachData(AgeRangeAttachData.Text, "19至29岁")]
From19To29,

[AttachData(AgeRangeAttachData.Text, "30岁及以上")]
Above29
}

public enum AgeRangeAttachData
{
Text
}

public static class AgeRangeExtensions
{
public static string GetText(this AgeRange range)
{
return range.GetAttachedData<string>(AgeRangeAttachData.Text);
}
}

枚举的每一项其实相当于一个只读的共有Field,也可以加上自定义属性。我们为AgeRange枚举的每一项加上一个以 AgeRangeAttachData为Key的附加值作为该枚举的显示文字,并且针对AgeRange编写一个扩展方法GetText。于是代码中就可 以使用以下方式来获取某个枚举值的显示文字了:

AgeRange.From19To29.GetText()

关于上面的使用方式,有几个需要提一下的地方:

  • 虽然可以使用任意类型作Key,但是一般建议使用枚举来作为一个强类型的Key。
  • 一般可以为扩展的内容再增加一个扩展方法来获取“同一组”附加数据,以供各个地方重复调用。
  • GetAttachedData使用反射并且遍历所有的AttachDataAttribute,如果您觉得需要提高性能,那么可以自 行添加缓存。
  • 以上使用方法在普通情况下可以接受,但其实它并不是Best Practice。一般来说,文字内容都应该提取至外部资源文件中。

以上是老赵项目中使用的一种扩展方式,  在此记录便于日后X 用

 

 

 

posted on 2010-07-02 11:26  布颜书  阅读(343)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3