几个常用的LINQ查询
2009-01-22 22:56
309 查看
用来备份,以后更新并查阅。:)
#region GetFiles
/// <summary>
/// 获得指定路径下的所有文件
/// </summary>
/// <param name="path">有效路经</param>
/// <returns></returns>
public IEnumerable<FileInfo> GetFiles(string path)
{
DirectoryInfo directory = new DirectoryInfo(path);
if (directory == null)
return null;
return
from f in directory.GetFiles()
select f;
}
/// <summary>
/// 测试GetFiles
/// </summary>
[TestMethod]
public void TestGetFiles()
{
string path = Environment.SystemDirectory;
var result = GetFiles(path);
Assert.IsNotNull(result);
foreach (var item in result)
{
Assert.IsInstanceOfType(item, typeof(FileInfo));
Assert.IsTrue(File.Exists(item.FullName));
}
}
#endregion
#region GetReturnSpecifiedInterfacedMethods
/// <summary>
/// 获得指定Assembly中返回指定接口类型名称的方法信息
/// </summary>
/// <param name="assembly">Assembly</param>
/// <param name="interfaceType">接口类型名称</param>
/// <returns></returns>
public IEnumerable<MethodInfo> GetReturnSpecifiedInterfacedMethods(Assembly assembly, string interfaceTypeName)
{
if (assembly == null) throw new ArgumentNullException("assembly");
if(string.IsNullOrEmpty(interfaceTypeName)) throw new ArgumentNullException("interfaceTypeName");
return
from type in assembly.GetTypes()
from method in type.GetMethods()
where
method.ReturnType.GetInterface(interfaceTypeName) != null
select method;
}
/// <summary>
/// 测试GetReturnSpecifiedInterfacedMethods
/// </summary>
[TestMethod]
public void TestGetReturnSpecifiedInterfacedMethods()
{
string typeName = "IEnumerable`1";
Assembly assembly = AppDomain.CurrentDomain.GetAssemblies()[0];
var result = GetReturnSpecifiedInterfacedMethods(assembly, typeName);
Assert.IsNotNull(result);
foreach (var item in result)
{
Assert.IsInstanceOfType(item, typeof(MethodInfo));
Assert.IsNotNull(item.ReturnType.GetInterface(typeName));
}
}
#endregion
#region GetAssemblyStaticMethods
/// <summary>
/// 获得指定Assembly包含的静态方法信息
/// </summary>
/// <param name="assembly"></param>
/// <returns></returns>
public IEnumerable<MethodInfo> GetAssemblyStaticMethods(Assembly assembly)
{
if (assembly == null) throw new ArgumentNullException("assembly");
return
from type in assembly.GetTypes()
from method in type.GetMethods()
where
method.IsStatic
select method;
}
/// <summary>
/// 测试GetAssemblyStaticMethods
/// </summary>
[TestMethod]
public void TestGetAssemblyStaticMethods()
{
Assembly assembly = AppDomain.CurrentDomain.GetAssemblies()[0];
var result = GetAssemblyStaticMethods(assembly);
Assert.IsNotNull(result);
foreach (var item in result)
{
Assert.IsInstanceOfType(item, typeof(MethodInfo));
Assert.IsTrue(item.IsStatic);
}
}
#region GetFiles
/// <summary>
/// 获得指定路径下的所有文件
/// </summary>
/// <param name="path">有效路经</param>
/// <returns></returns>
public IEnumerable<FileInfo> GetFiles(string path)
{
DirectoryInfo directory = new DirectoryInfo(path);
if (directory == null)
return null;
return
from f in directory.GetFiles()
select f;
}
/// <summary>
/// 测试GetFiles
/// </summary>
[TestMethod]
public void TestGetFiles()
{
string path = Environment.SystemDirectory;
var result = GetFiles(path);
Assert.IsNotNull(result);
foreach (var item in result)
{
Assert.IsInstanceOfType(item, typeof(FileInfo));
Assert.IsTrue(File.Exists(item.FullName));
}
}
#endregion
#region GetReturnSpecifiedInterfacedMethods
/// <summary>
/// 获得指定Assembly中返回指定接口类型名称的方法信息
/// </summary>
/// <param name="assembly">Assembly</param>
/// <param name="interfaceType">接口类型名称</param>
/// <returns></returns>
public IEnumerable<MethodInfo> GetReturnSpecifiedInterfacedMethods(Assembly assembly, string interfaceTypeName)
{
if (assembly == null) throw new ArgumentNullException("assembly");
if(string.IsNullOrEmpty(interfaceTypeName)) throw new ArgumentNullException("interfaceTypeName");
return
from type in assembly.GetTypes()
from method in type.GetMethods()
where
method.ReturnType.GetInterface(interfaceTypeName) != null
select method;
}
/// <summary>
/// 测试GetReturnSpecifiedInterfacedMethods
/// </summary>
[TestMethod]
public void TestGetReturnSpecifiedInterfacedMethods()
{
string typeName = "IEnumerable`1";
Assembly assembly = AppDomain.CurrentDomain.GetAssemblies()[0];
var result = GetReturnSpecifiedInterfacedMethods(assembly, typeName);
Assert.IsNotNull(result);
foreach (var item in result)
{
Assert.IsInstanceOfType(item, typeof(MethodInfo));
Assert.IsNotNull(item.ReturnType.GetInterface(typeName));
}
}
#endregion
#region GetAssemblyStaticMethods
/// <summary>
/// 获得指定Assembly包含的静态方法信息
/// </summary>
/// <param name="assembly"></param>
/// <returns></returns>
public IEnumerable<MethodInfo> GetAssemblyStaticMethods(Assembly assembly)
{
if (assembly == null) throw new ArgumentNullException("assembly");
return
from type in assembly.GetTypes()
from method in type.GetMethods()
where
method.IsStatic
select method;
}
/// <summary>
/// 测试GetAssemblyStaticMethods
/// </summary>
[TestMethod]
public void TestGetAssemblyStaticMethods()
{
Assembly assembly = AppDomain.CurrentDomain.GetAssemblies()[0];
var result = GetAssemblyStaticMethods(assembly);
Assert.IsNotNull(result);
foreach (var item in result)
{
Assert.IsInstanceOfType(item, typeof(MethodInfo));
Assert.IsTrue(item.IsStatic);
}
}
相关文章推荐
- C# DataTable几个常用的查询表达式【转】
- 常用的SQL语句<二>几个高级查询运算词
- 整理出的数据库中几个常用sql查询语句
- oracle常用的几个数据库信息查询
- 关于Mono使用linq查询的几个BUG
- 几个常用数据库查询限定行数 命令
- C#学习常用的命名空间---System.Data.Linq(语言集成查询)
- 查询XML内容中节点的几个常用方法
- linq与lambda 常用查询语句写法对比
- 常用的一些LINQ查询
- PG常用的几个查询(1)
- php中几个常用的数据库查询函数
- Linq常用查询运算符
- PG常用的几个查询(1)
- Ajax常用的几个函数及Alexa查询的几个查询接口及使用方法
- 查询用户信息的几个常用命令
- Linq的常用查询和操作总结实例
- LINQ to Entities 常用查询语句
- LINQ 常用查询
- 最常用的几个mysql查询