Tag: reflection

C# – 获取对象的引用数

我正在尝试为我正在写的小爱好游戏编写一个简单的资源管理器。 此资源管理器需要执行的任务之一是卸载未使用的资源。 我可以想到以两种方式做到这一点: 当一个对象不再需要对资源的引用时,它必须调用资源管理器的方法来表示它不再使用它; 要么 当一个对象不再需要对该资源的引用时,它只是将其设置为null。 然后,当要求资源管理器卸载未使用的资源时,它会获取每个资源的引用计数(通过reflection?)。 如果引用计数为1(资源管理器将具有对资源的引用),则卸载资源。 有没有办法在C#中实现第二个解决方案? 谢谢。

C# – 通过reflection获取简单类型的用户友好名称?

Type t = typeof(bool); string typeName = t.Name; 在这个简单的示例中, typeName的值为”Boolean” 。 我想知道是否/如何让它说”bool”而不是。 对于int / Int32,double / Double,string / String也是如此。

如何在C#中在运行时向类添加属性?

我上课了: class MyClass { } … MyClass c = new MyClass(); 是否可以在运行时向此类添加属性/字段? ( 我不知道它们在编译时的类型或名称是什么,它们没有我可以使用的通用接口。 ) 假的例子: Add property named “Prop1” [type System.Int32] Add property named “Prop900” [type System.String] 我已经阅读了这个问题,但它使用了界面 提前致谢。

创建自定义AppDomain并向其添加程序集

如何创建appdomain,向其添加程序集,然后销毁该app域? 这是我尝试过的: static void Main(string[] args) { string pathToExe = @”A:\Users\Tono\Desktop\ConsoleApplication1.exe”; AppDomain myDomain = AppDomain.CreateDomain(“MyDomain”); Assembly a = Assembly.Load(System.IO.File.ReadAllBytes(pathToExe)); myDomain.Load(a.FullName); // Crashes here! } 我也尝试过: myDomain.Load(File.ReadAllBytes(pathToExe)); 如何将程序集添加到appdomain。 一旦我这样做,我可以通过reflection找到方法执行它,然后销毁appdomain 我得到的例外是: 无法加载文件或程序集“ConsoleApplication1,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null”或其依赖项之一。 该系统找不到指定的文件。

测试Convert.ChangeType是否适用于两种类型

这是关于使用reflection转换值的问题的后续行动。 将某种类型的对象转换为另一种类型可以这样做: object convertedValue = Convert.ChangeType(value, targetType); 给定两个Type实例(比如FromType和ToType),有没有办法测试转换是否成功? 我可以写一个像这样的扩展方法: public static class TypeExtensions { public static bool CanChangeType(this Type fromType, Type toType) { // what to put here? } } 编辑:这就是我现在所拥有的。 丑陋,但我还没有看到另一种方式…… bool CanChangeType(Type sourceType, Type targetType) { try { var instanceOfSourceType = Activator.CreateInstance(sourceType); Convert.ChangeType(instanceOfSourceType, targetType); return true; // OK, it can be converted } […]

如何使用reflection递归打印对象属性的值

为了帮助调试我正在处理的一些代码,我开始编写一个方法来递归地打印出对象属性的名称和值。 但是,大多数对象都包含嵌套类型,我也想打印它们的名称和值,但仅限于我定义的类型。 这是我到目前为止的概述: public void PrintProperties(object obj) { if (obj == null) return; Propertyinfo[] properties = obj.GetType().GetProperties(); foreach (PropertyInfo property in properties) { if ([property is a type I have defined]) { PrintProperties([instance of property’s type]); } else { Console.WriteLine(“{0}: {1}”, property.Name, property.GetValue(obj, null)); } } 支架之间的部件是我不确定的地方。 任何帮助将不胜感激。

确定类型是否为静态

假设我有一个叫做type 。 我想确定我是否可以使用我的类型执行此操作(实际上不对每种类型执行此操作): 如果type是System.Windows.Point那么我可以这样做: Point point1 = new Point(); 但是,如果type是System.Environment那么这将不会飞: Environment environment1 = new Environment(); //wrong 因此,如果我遍历程序集中的每个可见类型,如何跳过将无法创建第二个实例的所有类型? 我对反思很陌生,所以我的术语还不是很好。 希望我在这里尝试做的很清楚。

如何获取调用方法的参数值?

题 我正在编写一些代码,需要能够从调用类的方法中获取参数的值 。 我知道如何一直到ParameterInfo []数组,但我不知道如何获取值。 这有可能吗? 如果是,我认为它与使用MethodInfo对象的MethodBody属性有关,它允许你检查IL流,包括属性,但我不知道怎么做,我还没有找到适用于Google的代码。 码 // Finds calling method from class that called into this one public class SomeClass { public static void FindMethod() { for (int i = 1; i < frameCount; i++) { var frame = new StackFrame(i); var methodInfo = frame.GetMethod(); if (methodInfo.DeclaringType != this.GetType()) { string methodName = […]

从.NET中的堆栈帧获取参数值?

我希望能够从.NET中的堆栈帧获取所有参数值。 有点像在Visual Studio调试器中看到调用堆栈中的值的方式。 我的方法集中在使用StackFrame类 ,然后反映在ParameterInfo数组上。 我在reflection和属性方面取得了成功,但事实certificate这有点棘手。 有没有办法实现这一目标? 到目前为止,代码如下所示: class Program { static void Main(string[] args) { A a = new A(); a.Go(1); } } public class A { internal void Go(int x) { B b = new B(); b.Go(4); } } public class B { internal void Go(int y) { Console.WriteLine(GetStackTrace()); } public static string […]

从base的类静态方法获取派生类类型

我想从其基类​​的静态方法获取派生类的类型。 如何实现这一目标? 谢谢! class BaseClass { static void Ping () { Type t = this.GetType(); // should be DerivedClass, but it is not possible with a static method } } class DerivedClass : BaseClass {} // somewhere in the code DerivedClass.Ping();