惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
罗磊的独立博客
博客园 - 【当耐特】
M
MIT News - Artificial intelligence
月光博客
月光博客
博客园_首页
博客园 - 叶小钗
T
Tailwind CSS Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
I
InfoQ
量子位
小众软件
小众软件
爱范儿
爱范儿
The GitHub Blog
The GitHub Blog
IT之家
IT之家
Jina AI
Jina AI
阮一峰的网络日志
阮一峰的网络日志
G
Google Developers Blog
WordPress大学
WordPress大学
人人都是产品经理
人人都是产品经理
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
J
Java Code Geeks
云风的 BLOG
云风的 BLOG
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报

博客园 - 超薄

符号执行虚假控制流去混淆 ios 重启usb ubuntu 搜狗输入法安装 js 反hook js 源码乱码 js和html加上混淆 js 字体加密 jj混淆 webpack 暴力方式扣取 志远js调试相关 并行算法中的异常 事件模式实现通知 位运算应用口诀和实例(转自大笨狼) 数据结构实际应用----订单排序(堆排序求前N大) 数据结构Tire 树实际应用----过滤禁词 动态规划 快速排序算法 字典 DictionaryBase 和 SortedList 模式匹配文本处理 位运算
扩展属性应用
超薄 · 2012-04-04 · via 博客园 - 超薄

字段去除空格

代码

 [AttributeUsage(AttributeTargets.Property,Inherited=false,AllowMultiple=false)]
public class TrimAttribute:Attribute
{ private readonly Type type;
public TrimAttribute(Type type)
{
this.type = type;
}
public Type Type
{
get { return this.type; }
}
}
public static class TrimAttributeExtension
{
public static void Trim(this Object obj)
{
Type t = obj.GetType();
foreach(var prop in t.GetProperties())
{
foreach(var attr in prop.GetCustomAttributes(typeof(TrimAttribute),true))
{
TrimAttribute tsa = (TrimAttribute)attr;
if(prop.GetValue(obj,null)!=null&&(tsa.Type==typeof(string)))
{
prop.SetValue(obj,GetPropertyValue(obj,prop.Name).ToString().Trim(),null);
}
}
}
}
private static object GetPropertyValue(object instance,string PropertName)
{
return instance.GetType().InvokeMember(PropertName,System.Reflection.BindingFlags.GetProperty,null,instance,new Object[]{});
}
}

使用

  public class SomeClass
{ [Trim(typeof(string))]
string Name { get; set; }
}


查询语法

 public  class SelectClass
{
public void Method1()
{
int[] fool=new int[100];
for (int num = 0; num < fool.Length; num++)
{
fool[num] = num * num;
}
foreach(int i in fool)
{
Console.WriteLine(i.ToString());
}
}
public void Method1Select()
{
int[] fool = (from n in Enumerable.Range(0, 100) select n * n).ToArray();
fool.ForAll((n) => Console.WriteLine(n));
}
public IEnumerable<Tuple<int, int>> ProduceIndices()
{
for (int x = 0; x < 100; x++)
for (int y = 0; y < 100; y++)
if (x + y < 100)
yield return Tuple.Create(x,y);
}
public IEnumerable<Tuple<int, int>> ProduceIndicesSelect()
{
return from x in Enumerable.Range(0, 100)
from y in Enumerable.Range(0, 100)
where x + y < 100
select Tuple.Create(x,y);
}
}

public static class Extensions
{
public static void ForAll<T>(this IEnumerable<T> sequence, Action<T> action)
{
foreach(T item in sequence)
{
action(item);
}
}
}