Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 是否获取与类型关联的所有名称空间?_C#_Reflection_Types_Namespaces - Fatal编程技术网

C# 是否获取与类型关联的所有名称空间?

C# 是否获取与类型关联的所有名称空间?,c#,reflection,types,namespaces,C#,Reflection,Types,Namespaces,有没有办法使用反射获取与类/类型关联的所有名称空间 例如,假设我的变量为: var list = List<MyClass> 我希望能够返回以下名称空间: MyCompany.MyClass(在MyClass中使用) System.Collections.Generic(列表正在使用它) 系统为int32(因为MyNumber正在使用它) 谢谢。您可以使用reflecion解析类型 由于框架设计,您将获得比预期多得多的结果 我希望我没有犯太多的错误 测试 名称空间控制台 {

有没有办法使用反射获取与类/类型关联的所有名称空间

例如,假设我的变量为:

var list = List<MyClass> 
我希望能够返回以下名称空间:

  • MyCompany.MyClass(在MyClass中使用)
  • System.Collections.Generic(列表正在使用它)
  • 系统为int32(因为MyNumber正在使用它)

谢谢。

您可以使用reflecion解析类型

由于框架设计,您将获得比预期多得多的结果

我希望我没有犯太多的错误

测试

名称空间控制台
{
公共课程
{
静态公共void Main(字符串[]args)
{
var list=新列表();
var typeSearched=list.GetType();
WriteLine($“{typeSearched.Name}需要这些名称空间:”;
foreach(typeSearched.GetUsingNamespaces().OrderBy(kvp=>kvp.Key)中的var项)
{
字符串名称=string.Join(Environment.NewLine+“”,
item.Value.Select(t=>t.Name)
.OrderBy(n=>n));
Console.WriteLine($”{item.Key}类型:“+Environment.NewLine”+
$“{names}”);
Console.WriteLine();
}
}
公共类MyClass
{
公共Int32 MyNumber{get;set;}
}
}
反射助手类

static public void AddNoDuplicates<T>(this IList<T> list, T value)
{
  if ( !list.Contains(value) ) list.Add(value);
}

static public void AddNoDuplicates<TKey, TValue>(this IDictionary<TKey, List<TValue>> list,
                                                 TKey key, TValue value)
{
  if ( key == null ) return;
  if ( !list.ContainsKey(key) )
    list.Add(key, new List<TValue>() { value });
  else
  if ( !list[key].Contains(value) )
    list[key].Add(value);
}

static public void AddRangeNoDuplicates<T>(this IList<T> list, IEnumerable<T> collection)
{
  foreach ( T value in collection )
    if ( !list.Contains(value) )
      list.Add(value);
}
使用系统;
使用System.Linq;
使用System.Collections.Generic;
运用系统反思;
名称空间控制台
{
静态公共类reflectionhelper
{
//见下文
}
}
工具方法

static public void AddNoDuplicates<T>(this IList<T> list, T value)
{
  if ( !list.Contains(value) ) list.Add(value);
}

static public void AddNoDuplicates<TKey, TValue>(this IDictionary<TKey, List<TValue>> list,
                                                 TKey key, TValue value)
{
  if ( key == null ) return;
  if ( !list.ContainsKey(key) )
    list.Add(key, new List<TValue>() { value });
  else
  if ( !list[key].Contains(value) )
    list[key].Add(value);
}

static public void AddRangeNoDuplicates<T>(this IList<T> list, IEnumerable<T> collection)
{
  foreach ( T value in collection )
    if ( !list.Contains(value) )
      list.Add(value);
}
测试输出已满

#类型的ConsoleApp:
类名
MyClass[]
#类型的系统:
行动'1
排列
布尔值
比较'1
转换器'2
易克隆的
不可比
i可比'1
i可比'1
i可比'1
易变
可识别
可数'1
可数'1
可数'1
可攻击的
Int32
对象
谓语'1
一串
类型
无效的
#类型的系统集合:
I收集
数不清
迭代器
伊里斯特
峡部结构可比
峡部结构可调
#类型的System.Collections.Generic:
统计员
I集合'1
I集合'1
I集合'1
I集合'1
I比较者'1
IEnumerable`1
IEnumerable`1
IEnumerable`1
IEnumerable`1
IEnumerable`1
IEnumerator`1
IEnumerator`1
IList`1
IList`1
IList`1
IList`1
IReadOnlyCollection`1
IReadOnlyCollection`1
IReadOnlyCollection`1
IReadOnlyCollection`1
IReadOnlyList`1
IReadOnlyList`1
IReadOnlyList`1
IReadOnlyList`1
清单` 1
清单` 1
清单` 1
清单` 1
T
T
T[]
头饰
#类型的System.Collections.ObjectModel:
只读集合'1
#类型的系统反射:
ICustomAttributeProvider
直射
#类型的System.Runtime.InteropServices:
_会员信息
_类型
#类型的System.Runtime.Serialization:
ISerializable

某些类型名称重复,但类型实例不同…

通过使用某种严重级别的反射和嵌套循环,您可以实现这一点。您能解释一下需要您执行此操作的用例吗?
List
有自己的字段……但也应该考虑它们,不是吗?(虽然这不会影响返回的名称空间集)@ChetanRanpariya我需要在运行时(即Workflow Foundation)将每个单独的名称空间导入到项目中,首先您可以执行
var type=list.GetType();var nameSpace=type.nameSpace;
然后需要使用更多的反射功能来获取字段、方法以及这些字段、方法的类型和类型,然后获取这些类型的名称空间。使用
list.GetType
来发现其类型。然后检查它是否为参数化类型,并获取其第一个类型参数以获取MyClass。获取
MyClass
的属性及其类型。这一切都非常简单。当您使用反射时,最好在小范围内完成(快速编译和调试)测试程序。有很多探索性的试验和错误。很抱歉耽搁了!不得不做些别的。回答得很好!这肯定给了我一个很好的起点!
static public Dictionary<string, List<Type>> GetUsingNamespaces(this Type typeSearched)
{
  var result = new Dictionary<string, List<Type>>();
  result.AddNoDuplicates(typeSearched.Namespace, typeSearched);
  foreach ( Type type in typeSearched.GetMembersTypes() )
  {
    result.AddNoDuplicates(type.Namespace, type);
    foreach ( var implement in type.GetInterfaces() )
      result.AddNoDuplicates(implement.Namespace, implement);
    foreach ( var argument in type.GetGenericArguments() )
      result.AddNoDuplicates(argument.Namespace, argument);
  }
  return result;
}
static public List<Type> GetMembersTypes(this Type type)
{
  var flags = BindingFlags.Static
            | BindingFlags.Instance
            | BindingFlags.Public
            | BindingFlags.NonPublic;
  var result = new List<Type>();
  foreach ( var field in type.GetFields(flags) )
    result.AddNoDuplicates(field.FieldType);
  foreach ( var property in type.GetProperties(flags) )
    result.AddNoDuplicates(property.PropertyType);
  foreach ( var ev in type.GetEvents(flags) )
    result.AddNoDuplicates(ev.EventHandlerType);
  foreach ( var method in type.GetMethods(flags) )
  {
    result.AddNoDuplicates(method.ReturnType);
    foreach ( var a in method.GetParameters() )
      result.AddNoDuplicates(a.ParameterType);
  }
  foreach ( var constructor in type.GetConstructors() )
    foreach ( var param in constructor.GetParameters() )
      result.AddNoDuplicates(param.ParameterType);
  foreach ( var nested in type.GetNestedTypes(flags) )
    result.AddRangeNoDuplicates(GetMembersTypes(nested));
  return result;
}