[索引页]



今天有空就研究一下LINQ和泛型,Reflection的应用,看见一篇很好的文章就翻译过拉!


LINQ to Reflection
地址



LINQ是很好的东西,允许我们查任何实现IEnumerable<T>对象.其中包括List<T>,Collection<T>,和其他更多的.很多查询操作符号都是拉反射返回数组,接下来我使用的这些LINQ 操作符就是.


Loading up assemblies for searching




首先,我想加载一些assemblies去搜索,这很容易的就发现使用System.Reflection.Assembly.LoadWithPartialName,可是这个方法是.NET推荐使用的方法.但这个方法将满足简单达到搜索要求.我不想使用写代码的方式.

首先带来的就是一个要搜索的assembly名的清单并把他们放入的数组.

;

都是非长漂亮的.NET Framework 3.5 assemblies,下一步,我要创建一个LINQ 查询去查询加载上的的assemblies,所以我能执行搜索这些assemblies.这是使用扩展方法:

LINQ to Reflection(翻译)var assemblies = assemblyNames.Select(name => Assembly.LoadWithPartialName(name));


下面是查询语法方式:

LINQ to Reflection(翻译)var assemblies = from name in assemblyNames
LINQ to Reflection(翻译)            select Assembly.LoadWithPartialName(name);


assemblies是变量,我们能在被加载assemblies中的执行查询.


Finding generic delegates




我已经体验一个API,并且如果我要知道现在有的.NET中的generic delegate.我们使用LINQ 表达方式很轻易的从已经加载的assemblies中得到.但首先,我需要知道怎样查找.我只能加载generic delegate到已经知道Type object中.

LINQ to Reflection(翻译)Type actionDelegate = typeof(Action<>);


当我加载一个generic delegate 进入一个Type对象中时,我注意一些关键属性,如"IsGenericType" and "IsPublic"这两个属性能决定它是否是generic,但不能决定是delegate,不幸运的是没有"IsDelegate",但我们可以转而使用IsSubclassOf(typeof(Delegate))返回"true".综合上面三个条件就能判定,我已经有一个predicate在我的搜索assemblies中使用.

LINQ to Reflection(翻译)var types = from name in assemblyNames
LINQ to Reflection(翻译)            select Assembly.LoadWithPartialName(name) into a
LINQ to Reflection(翻译)            from c 
in a.GetTypes()
LINQ to Reflection(翻译)            
where c.IsGenericType && c.IsPublic && c.IsSubclassOf(typeof(Delegate))
LINQ to Reflection(翻译)            select c;
LINQ to Reflection(翻译)
LINQ to Reflection(翻译)
foreach (Type t in types)
}


这里是LINQ同过一个附加部分("into a")结合在一起.这里首先查询枚举assembly名查询加载到assemblies.第二个查询在每一个assembly中调用"GetType"去查询加载的assembly类型,并且使用"where" clause在里面填充predicate就能很容易的筛选generic delegates.下面片段就结果.

LINQ to Reflection(翻译)System.EventHandler`1[TEventArgs]
LINQ to Reflection(翻译)System.Action`
1[T]
LINQ to Reflection(翻译)System.Comparison`
1[T]
LINQ to Reflection(翻译)System.Converter`
2[TInput,TOutput]
LINQ to Reflection(翻译)System.Predicate`
1[T]
LINQ to Reflection(翻译)System.Linq.Func`
1[TResult]
LINQ to Reflection(翻译)System.Linq.Func`
2[TArg0,TResult]
LINQ to Reflection(翻译)System.Linq.Func`
3[TArg0,TArg1,TResult]
LINQ to Reflection(翻译)System.Linq.Func`
4[TArg0,TArg1,TArg2,TResult]
LINQ to Reflection(翻译)System.Linq.Func`
5[TArg0,TArg1,TArg2,TArg3,TResult]


但并不是全部,但已经有的已经在这里拉.特别的是,我已经发现重载Action<T>,如同如果像Func<TResult>有多个委托声明时候样,可是这里没有,但你可以到计划重载Action匹配func.


Context 和 IDisposable




(I had a discussion with a team member that centered around types with names postfixed with "Context".)
已经讨论以命名后缀"Context'为中心的类型一组成员.我说明命名"Context"的类型隐含实现拉IDisposable,它被用来创建一个scope.
我只有通过得到一些真实的数据并找到在.NET Framework中被命名为"Context"还实现IDisposabble的来说明推测.下面是LINQ query

LINQ to Reflection(翻译)var types = from name in assemblyNames
LINQ to Reflection(翻译)            select Assembly.LoadWithPartialName(name) into a
LINQ to Reflection(翻译)            from c 
in a.GetTypes()
LINQ to Reflection(翻译)            
where (c.IsClass || c.IsInterface) && c.FullName.EndsWith("Context")
LINQ to Reflection(翻译)            group c.FullName by c.GetInterfaces().Contains(
typeof(IDisposable)) into g
;


在查询中,我要查找所有以"Context"结尾的类型,并且按照是否实现IDisposable返回二进制的方式分组.并且我还取两个数据组合,并且anonymous types持有数据.下面可以输出结果.


LINQ to Reflection(翻译)foreach (var g in types)
}

并且最被找到说明我的论点不被支持.下面是输出结果.

LINQ to Reflection(翻译)144 types where IsIDisposable is False
LINQ to Reflection(翻译)
50 types where IsIDisposable is True


我的左侧输出打印出所有的Context IDisposable类型,清单相当的长.我决定没有过滤non-public 类型,如MS 趋向产生有很多"internal"类型.所以它输出只有25%的以"Context"类型结尾的类型实现IDisposable,所以我的推断是错误的.


Other applications




LINQ provides a clean syntax to search assemblies using reflection.  I've also used it to argue against read-write properties for array types (they account for only 0.6% of all collection-type properties).  The continuations and joining lower the barrier for searching for specific types, properties, etc.  Since the LINQ methods are extension methods for any IEnumerable<T> type, you'll be pleasantly surprised when IntelliSense kicks in at the options available for querying and manipulating collections and arrays.



works guo 翻译

相关文章:

  • 2021-11-29
  • 2021-09-21
  • 2021-04-14
  • 2021-09-03
  • 2021-08-07
  • 2021-11-08
  • 2021-06-18
  • 2021-11-04
猜你喜欢
  • 2021-10-22
  • 2022-12-23
  • 2022-02-16
  • 2021-06-28
  • 2022-02-08
  • 2021-10-02
  • 2022-01-07
相关资源
相似解决方案