gpt4 book ai didi

c# - 获取与类型关联的所有 namespace ?

转载 作者:行者123 更新时间:2023-12-04 16:50:47 24 4
gpt4 key购买 nike

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

例如,假设我的变量是:

var list = List<MyClass> 

我的类(class)定义为

public class MyClass
{
property int32 MyNumber {get; set;}
}

我希望能够返回以下命名空间:

  • MyCompany.MyClass(因为我的 MyClass 正在使用它)
  • System.Collections.Generic(因为它被 List 使用)
  • 系统为 int32(因为它被 MyNumber 使用)

谢谢。

最佳答案

您可以使用反射来解析类型。

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

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

测试

namespace ConsoleApp
{
public class Program
{
static public void Main(string[] args)
{
var list = new List<MyClass>();
var typeSearched = list.GetType();
Console.WriteLine($"{typeSearched.Name} needs these namespaces:");
foreach ( var item in typeSearched.GetUsingNamespaces().OrderBy(kvp => kvp.Key) )
{
string names = string.Join(Environment.NewLine + " ",
item.Value.Select(t => t.Name)
.OrderBy(n => n));
Console.WriteLine($"# {item.Key} for type(s):" + Environment.NewLine +
$" {names}");
Console.WriteLine();
}
}
public class MyClass
{
public Int32 MyNumber {get; set;}
}
}

反射辅助类

using System;
using System.Linq;
using System.Collections.Generic;
using System.Reflection;

namespace ConsoleApp
{
static public class ReflexionHelper
{
// See below
}
}

工具方法

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);
}

解析类型的方法

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;
}

future 的改进

分析属性和其他被遗忘的东西。

测试输出短路

ConsoleApp
System
System.Collections
System.Collections.Generic
System.Collections.ObjectModel
System.Reflection
System.Runtime.InteropServices
System.Runtime.Serialization

测试输出完整

# ConsoleApp for type(s):
MyClass
MyClass[]

# System for type(s):
Action`1
Array
Boolean
Comparison`1
Converter`2
ICloneable
IComparable
IComparable`1
IComparable`1
IComparable`1
IConvertible
IDisposable
IEquatable`1
IEquatable`1
IEquatable`1
IFormattable
Int32
Object
Predicate`1
String
Type
Void

# System.Collections for type(s):
ICollection
IEnumerable
IEnumerator
IList
IStructuralComparable
IStructuralEquatable

# System.Collections.Generic for type(s):
Enumerator
ICollection`1
ICollection`1
ICollection`1
ICollection`1
IComparer`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
List`1
List`1
List`1
List`1
T
T
T[]
TOutput

# System.Collections.ObjectModel for type(s):
ReadOnlyCollection`1

# System.Reflection for type(s):
ICustomAttributeProvider
IReflect

# System.Runtime.InteropServices for type(s):
_MemberInfo
_Type

# System.Runtime.Serialization for type(s):
ISerializable

某些类型名称重复但 Type 实例不同...

关于c# - 获取与类型关联的所有 namespace ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62856831/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com