Tag: reflection

C#reflection和查找所有引用

鉴于一个DLL文件,我希望能够find该DLL文件中的方法的所有调用。 我该怎么做? 从本质上讲,我怎样才能以编程方式做Visual Studio已经做了什么? 我不想使用像.NET Reflector这样的工具来做到这一点,但reflection是好的,可能是必要的。

从方法中检索调用方法名称

可能重复: 我怎样才能find调用当前方法的方法? 我有一个对象的方法在对象内的多个地方被调用。 有没有一个简单快捷的方法来获得这个方法的名字,这个方法叫做这个stream行的方法。 伪代码示例: public Main() { PopularMethod(); } public ButtonClick(object sender, EventArgs e) { PopularMethod(); } public Button2Click(object sender, EventArgs e) { PopularMethod(); } public void PopularMethod() { //Get calling method name } 在PopularMethod()我想看到Main的价值,如果它从Main调用…我想看到“ ButtonClick ”,如果从ButtonClick调用ButtonClick PopularMethod() 我在看System.Reflection.MethodBase.GetCurrentMethod()但不会让我的调用方法。 我已经看了StackTrace类,但是我真的不喜欢每次调用这个方法时运行整个堆栈跟踪。

使用reflection在C#中创build没有默认构造函数的types实例

以下面的课程为例: class Sometype { int someValue; public Sometype(int someValue) { this.someValue = someValue; } } 然后我想使用reflection来创build这种types的实例: Type t = typeof(Sometype); object o = Activator.CreateInstance(t); 通常这是可行的,但是因为SomeType没有定义一个无参数的构造函数,所以对Activator.CreateInstance的调用会抛出一个types为MissingMethodException的exception,消息是“ 没有为这个对象定义无参数的构造函数 ” 。是否还有另一种方法来创build这种types的实例? 将无参数的构造函数添加到我的所有类中会有点儿麻烦。

如何检查一个types是一个子types还是一个对象的types?

要检查一个types是否是C#中另一个types的子类,很容易: typeof (SubClass).IsSubclassOf(typeof (BaseClass)); // returns true 但是,这将失败: typeof (BaseClass).IsSubclassOf(typeof (BaseClass)); // returns false 有没有什么办法来检查一个types是否是基类自身的子类,不使用OR运算符或使用扩展方法?

如何获得Python中当前模块中的所有类的列表?

我已经看到很多人从一个模块中提取所有类的例子,通常是这样的: # foo.py class Foo: pass # test.py import inspect import foo for name, obj in inspect.getmembers(foo): if inspect.isclass(obj): print obj 真棒。 但是我不知道如何从当前模块中获取所有的类。 # foo.py import inspect class Foo: pass def print_classes(): for name, obj in inspect.getmembers(???): # what do I do here? if inspect.isclass(obj): print obj # test.py import foo foo.print_classes() 这可能是很明显的,但我一直没有find任何东西。 谁能帮我吗?

比.NET Reflector更好的东西?

我曾经喜欢reflection回来的一天,但自从RedGate接手后,它急剧下滑。 现在它迫使我更新(这是绝对荒谬的),有一半的时间更新不顺利,每次更新都越来越妨碍我的生产力。 我厌倦了,而且我准备好了一些更好的东西。 有谁知道更好的反汇编? 更新: 答案中提到的各种备选scheme列表 – ILSpy dotPeek JustDecompile DisSharper 单音塞西尔 基洛夫州 Dotnet IL编辑器(DILE) 通用编译器基础结构

获取类path中的所有类

如何在运行时获得CLASSPATH中所有可用类的列表? 在Eclipse IDE中,可以通过按Ctrl + Shift + T来完成 。 在Java中有任何方法来完成它?

c#从string实例化类

我有一个抽象类,我想把它扩展到一个扩展它的类。 我有孩子类名string。 除此之外… String childClassString; MyAbstractClass myObject; if (childClassString = "myExtenedObjectA") myObject = new ExtenedObjectA(); if (childClassString = "myExtenedObjectB") myObject = new ExtenedObjectB(); 我该怎么做? 基本上我怎么摆脱这里的if语句?

你如何得到一个variables的名称,因为它的声明是物理types的?

可能重复: 在C#中查找传递给函数的variables名 下面的课程包含实地城市。 我需要dynamic确定字段的名称,因为它是在类声明中键入的,即我需要从对象城市的实例中获取string“城市”。 我试图通过检查其在DoSomething()中的types来做到这一点,但在debugging器中检查types的内容时找不到它。 可能吗? public class Person { public string city = "New York"; public Person() { } public void DoSomething() { Type t = city.GetType(); string field_name = t.SomeUnkownFunction(); //would return the string "city" if it existed! } } 有些人在下面的答案中问我为什么要这样做。 这是为什么。 在我现实世界的情况下,城市上面有一个自定义的属性。 [MyCustomAttribute("param1", "param2", etc)] public string city = "New York"; 我需要在其他代码中的这个属性。 […]

Java – 获取JVM中加载的所有类的列表

我想列出属于某个包裹的所有课程以及他们所有的孩子。 这些类可能已经或可能未被加载到JVM中。