标签: system.reflection

为什么 Assembly.GetTypes() 需要引用?

我想从我的程序集中获取所有类型,但我没有引用,我也不关心它们。查找接口类型与引用有什么关系?我有办法解决这个问题吗?

Assembly assembly = Assembly.LoadFrom(myAssemblyPath);
Type[] typeArray = assembly.GetTypes();
Run Code Online (Sandbox Code Playgroud)

抛出: FileNotFoundException 无法加载文件或程序集“某些引用的程序集”或其依赖项之一。该系统找不到指定的文件。

c# reflection types system.reflection

7
推荐指数
1
解决办法
2910
查看次数

生成 .NET 库公共 API 的文本文件,用于版本控制和兼容性跟踪

我维护了太多的 NuGet 包,并且我正在尝试找到一个工具来为每个程序集生成公共 API 表面的纯文本文件(如构建后步骤)。每个命名空间、类、接口、结构、方法签名、成员、字段都将是一行,全部按字母顺序排序。

每当我更改公共 API 界面时,文件都会发生src/PublicAPIs.txt更改,这将是令人惊奇的 - github diff 会立即向我显示我修改、删除或添加的内容,并且该文件对于跟踪 API 随着时间的推移发生的变化非常宝贵。

我认为,我不太可能意外暴露私有 API 或破坏现有 API。

我觉得这一定已经存在了,我只是错过了一些东西?我知道 Telerik JustAssembly 可以进行基本的 .dll 比较,但我正在寻找能够自动将文件写入 git 存储库的东西,这样我就不必记住打开工具,并且任何重大更改都会在我的过程中弹出。正常工作流程。

.net c# system.reflection

7
推荐指数
1
解决办法
740
查看次数

如何获取类型中的方法

给定:System.Type实例.

目的是获取类型中新引入的方法(我不知道正确的单词),这些方法是 - 不是继承的 - 不会被覆盖

我想使用.NET Reflection,我尝试了这个Type.GetMethods()方法.但是,它甚至返回了继承和被覆盖的.

在获得所有方法后我想到了过滤.我查看了MethodInfo类暴露的属性/方法.我无法想象得到我想要的东西.

例如:我有一节课, class A { void Foo() { } }

当我祈求typeof(A).GetMethods(),我得到Foo一起的方法System.Object:Equals,ToString,GetTypeGetHashCode.我想将其过滤掉Foo.

有谁知道如何做到这一点?

谢谢.

.net c# system.reflection

6
推荐指数
1
解决办法
7628
查看次数

Assembly.LoadFile中的OutOfMemoryException

我对这个很难过.有时候,当我打电话Reflection.Assembly.LoadFile("path to my dll")时会抛出一个OutOfMemoryException.

StackTrace只显示:

at System.Reflection.RuntimeAssembly.nLoadFile(String path, Evidence evidence)
at System.Reflection.Assembly.LoadFile(String path)
Run Code Online (Sandbox Code Playgroud)

并且没有InnerException检查更详细的异常信息.

但是,似乎只通过COM.

让我解释:

我的应用程序可以作为常规旧的exe启动,或通过COM接口通过Excel启动(CreateObject("blahblahblah"))

在初始化我的一个GUI类时,它将静态加载我的所有引擎DLL(我的应用程序有一个引擎和一个GUI)

总共有6个引擎DLL - 静态加载的汇编代码在VB.NET中,看起来有点像这样:

Private Shared dllPath As String = System.IO.Path.GetDirectoryName(Reflection.Assembly.GetExecutingAssembly().Location) & System.IO.Path.DirectorySeparatorChar)
Private Shared ReadOnly engineDLL1 As Reflection.Assembly = Reflection.Assembly.LoadFile(dllPath + "engineDLL1_name.dll")
...
Private Shared ReadOnly engineDLL6 As Reflection.Assembly = Reflection.Assembly.LoadFile(dllPath + "engineDLL6_name.dll")
Run Code Online (Sandbox Code Playgroud)

我试图加载的引擎DLL是混合程序集(包含本机和托管C++) - 不确定是否重要.

无论如何,使用procexp,我注意到以标准方式打开我的应用程序,所有这些静态加载的DLL都加载正常.但是,如果我通过COM(通过Excel CreateObject("ProgID of the class with the code above"))初始化此类,它将抛出OutOfMemoryException引擎DLLL4.

如果有人可以提供任何见解,那将非常感谢!!

vb.net com c++-cli out-of-memory system.reflection

6
推荐指数
0
解决办法
470
查看次数

从Attribute转到CustomAttributeData或向后

题.有没有办法CustomAttributeData根据自定义属性的给定实例获取实例,比如说MyAttribute?或相反亦然?

我为什么需要这个?实例MyAttribute包含我感兴趣的属性,同时实例CustomAttributeData包含实际的构造函数的参数我很感兴趣,所以,现在我实现了双重的工作:第一,得到的实例MyAttribute调用

Attribute.GetCustomAttribute(property, typeof(MyAttribute)) as MyAttribute
Run Code Online (Sandbox Code Playgroud)

,第二,CustomAttributeData通过调用获取实例

CustomAttributeData.GetCustomAttributes(property)
Run Code Online (Sandbox Code Playgroud)

并走过这个系列.

PS我已经看过这个问题,但没有在那里找到理想的解决方案.

c# reflection custom-attributes system.reflection

6
推荐指数
1
解决办法
2427
查看次数

如何在不使用C#中的反射的情况下从该方法中获取方法名称

我想从内部获取方法名称.这可以使用reflection如下所示完成.但是,我想在不使用的情况下得到它reflection

System.Reflection.MethodBase.GetCurrentMethod().Name 
Run Code Online (Sandbox Code Playgroud)

示例代码

public void myMethod()
{
    string methodName =  // I want to get "myMethod" to here without using reflection. 
}
Run Code Online (Sandbox Code Playgroud)

c# methods system.reflection

6
推荐指数
2
解决办法
3357
查看次数

FieldInfo更新字段的子字段

美好的一天,

我需要创建将在存储变量名和变量的新值的Dictionary上迭代的函数.之后,我需要用该值更新类变量.

void UpdateValues(Type type, Dictionary<string, string> values)
{
    foreach (var value in values)
    {
        var fieldInfo = selected.GetComponent(type).GetType().GetField(value.Key);
        if (fieldInfo == null) continue;

        fieldInfo.SetValue(selected.GetComponent(type), value.Value);
    }
}
Run Code Online (Sandbox Code Playgroud)

它有效,但我想要一点改进,我绝对不知道是否可能.如您所见,该函数可以接受任何类,而不仅仅是一个类.

如果我有这样的课

class test
{
    public string age;
}
Run Code Online (Sandbox Code Playgroud)

我会以这种方式使用函数,它会工作.

UpdateValues(typeof(test), new Dictionary<string, string>{{"age", "17"}});
Run Code Online (Sandbox Code Playgroud)

问题是,如果我有这样的类,我想更新"子字段"(字段中的字段)

class test
{
    public string age;
}

class test2
{
    public test data;
}
Run Code Online (Sandbox Code Playgroud)

我认为语法可能是这样的,但我不知道我怎么能这样做.

UpdateValues(typeof(test2), new Dictionary<string, string>{{"data.age", "17"}});
Run Code Online (Sandbox Code Playgroud)

总结一下,我需要创建一个函数来获取存储在另一个类中的类.函数将通过字典迭代并更新类甚至是子字段的字段.

.net c# reflection system.reflection

6
推荐指数
1
解决办法
301
查看次数

在用户定义的程序集下获取所有类型

我试图获取在特定用户定义的命名空间下定义的所有类型

Assembly.GetEntryAssembly().GetTypes().Where(t => t.Namespace == "namespace")


    <>c__DisplayClass3_0    
    <>c__DisplayClass4_0    
    <>c__DisplayClass6_0    
    <>c__DisplayClass2_0    
    <>c__DisplayClass2_1    
    <>c__DisplayClass2_2    
    <>c__DisplayClass2_3    
    <>c__DisplayClass2_4    
    <>c__DisplayClass2_5    
    <>c__DisplayClass2_6    
    <>c__DisplayClass2_7    
    <>c__DisplayClass2_8
Run Code Online (Sandbox Code Playgroud)

我的问题为什么我得到这些额外的类型没有在该命名空间下定义?

如何选择用户定义类型的类型?

有人解释了这些是什么以及如何在用户定义的命名空间中定义它们.

c# reflection system.reflection .net-core

6
推荐指数
1
解决办法
768
查看次数

类型“System.Int16”的对象无法转换为类型“System.Nullable`1[System.Int32]

我有一个方法可以从数据读取器的数据生成类类型列表。

if (datareader != null && datareader .HasRows)
{
  Dictionary<string, PropertyInfo> pDict= GetPropertyDictionary<T>();
  var fields = GetFieldNames(datareader );
  while (datareader .Read())
  {
    T myobj= new T();
    for (int index = 0; index < fields.Count; index++)
    {                        
      if (pDict.TryGetValue(fields[index], out PropertyInfo info))
      {
        var val1 = datareader .GetValue(index);                                
        info.SetValue(myobj, (val1 == DBNull.Value) ? null : val1, null);
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我有类属性,其中一些可以为空。

public string StudentName{ get; set; }
public decimal? percentage{ get; set; }
public int? StudentNumber{ get; set; }
Run Code Online (Sandbox Code Playgroud)

代码适用于除 …

c# nullable datareader system.reflection

6
推荐指数
1
解决办法
2万
查看次数

由于缺少程序集(.NET core),通过 MetadataLoadContext 检查类型失败

我想检查程序集是否具有特定类型,而不在当前范围内加载程序集,这可以通过 .NET Core 3 中的 MetadataLoadContext 获得。

但如果我尝试下面的例子

internal static class Program
{
    // ReSharper disable once UnusedParameter.Local
    private static void Main(string[] args)
    {
        var paths = new string[] { @"Plugin.dll" };
        var resolver = new PathAssemblyResolver(paths);
        var pluginInterface = typeof(IPlugin);
        using (var context = new MetadataLoadContext(resolver))
        {
            var assembly =
                context.LoadFromAssemblyName(@"Plugin");
            foreach (var type in assembly.GetTypes())
            {
                if (type.IsClass && pluginInterface.IsAssignableFrom(type))
                    Console.WriteLine("found");
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我得到一个例外

System.IO.FileNotFoundException:找不到核心程序集。在 MetadataLoadContext 构造函数中指定有效的核心程序集名称,或者提供可以加载核心程序集的 MetadataAssemblyResolver。

var context = new MetadataLoadContext(resolver)

核心组件是什么意思?或者我做错了什么? https://blog.vincentbitter.nl/net-core-3-0/ …

c# system.reflection .net-core

6
推荐指数
1
解决办法
2908
查看次数