reflection

How can I get all the inherited classes of a base class? [duplicate]

↘锁芯ラ 提交于 2020-01-01 04:30:06
问题 This question already has answers here : How to find all the types in an Assembly that Inherit from a Specific Type C# (4 answers) Get all derived types of a type (8 answers) Closed 6 years ago . class Foo { } class Foo1 : Foo { } class Foo2 : Foo { } How would I be able to get all the classes that use Foo as a base class? The inherited classes aren't necessary in the same assembly. 回答1: This is not fast, but as long as Foo is a concrete type (not an interface), then it should work. Foo

How do I add all EntityTypeConfiguration<> from current assembly automatically?

会有一股神秘感。 提交于 2020-01-01 04:27:06
问题 How do I add all EntityTypeConfiguration<> from current assembly automatically? public class Entities : DbContext { public Entities() : base("Entities") { } public virtual DbSet<User> Users { get; set; } // ... protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); foreach(var configuration in this.GetAllConfigurations()) { modelBuilder.Configurations.Add(configuration); } } private ... GetAllConfigurations() {

Any solution for Class.getMethod() reflection and autoboxing?

戏子无情 提交于 2020-01-01 04:04:08
问题 I want to use Class.getMethod(String name, Class... parameterTypes) to find the method I need to invoke with the given parameters, but apparently as described in Bug 6176992 Java doesn't include autoboxing there. So if my reflected class has a method with a (String, int) signature you still get a NoSuchMethodException with a {String.class, Integer.class} array as a paremeter. Is there any sollution for this? The only way I could think of to call getMethod() with every permutation of primitive

Checking a class type (.class) is equal to some other class type

♀尐吖头ヾ 提交于 2020-01-01 03:50:14
问题 Is the following code valid? void myMethod (Class classType) { if (classType == MyClass.class) { // do something } } myMethod (OtherClass.class); If not is there any other approach where I can check if a passed .class (Class Type) is of type - MyClass ? 回答1: Yes, that code is valid - if the two classes have been loaded by the same classloader. If you want the two classes to be treated as equal even if they've been loaded by different classloaders, possibly from different locations, based on

ASP.NET MVC - Use Reflection to find if a Controller Exists

霸气de小男生 提交于 2020-01-01 03:21:33
问题 I'm having a heck of a time figuring out how to properly implement my 404 redirecting. If I use the following <HandleError()> _ Public Class BaseController : Inherits System.Web.Mvc.Controller ''# do stuff End Class Then any unhandled error on the page will load up the "Error" view which works great. http://example.com/user/999 (where 999 is an invalid User ID) will throw an error while maintaining the original URL (this is what I want) However. If someone enters http://example.com/asdfjkl

What technique does Snoop uses to inspect a WPF application

岁酱吖の 提交于 2019-12-31 21:57:11
问题 Snoop, the spy utility, uses some powerful technique (probably some sort of reflection) to inspect a running WPF application. Most interesting is the fact, that Snnop is able to readout the entire object structure. A few days ago I downloaded the Snoop source code and spent some time on studying the internal behavior. Unfortunately, I couldn't find out yet how Snoop is doing these things, so I hope that anybody can help me out. At work I am currently writing a Coded UI Testing-Framework and

What technique does Snoop uses to inspect a WPF application

岁酱吖の 提交于 2019-12-31 21:57:09
问题 Snoop, the spy utility, uses some powerful technique (probably some sort of reflection) to inspect a running WPF application. Most interesting is the fact, that Snnop is able to readout the entire object structure. A few days ago I downloaded the Snoop source code and spent some time on studying the internal behavior. Unfortunately, I couldn't find out yet how Snoop is doing these things, so I hope that anybody can help me out. At work I am currently writing a Coded UI Testing-Framework and

How do i use Activator.CreateInstance with strings?

て烟熏妆下的殇ゞ 提交于 2019-12-31 19:49:29
问题 In my reflection code i hit a problem with my generic section of code. Specifically when i use a string. var oVal = (object)"Test"; var oType = oVal.GetType(); var sz = Activator.CreateInstance(oType, oVal); Exception An unhandled exception of type 'System.MissingMethodException' occurred in mscorlib.dll Additional information: Constructor on type 'System.String' not found. I tried this for testing purposes and it occurs in this single liner too var sz = Activator.CreateInstance("".GetType(),

What's the difference or relationship between Type and TypeInfo?

谁说我不能喝 提交于 2019-12-31 17:51:19
问题 I cannot fully understand when to use one or another. They are so similar that it's confusing me every now and them. When it comes to reflection, I don't know whether to use the plain Type or TypeInfo. .NET Framework itself adds more confusion with things like assembly.DefinedTypes . It retrieves an IEnumerable<TypeInfo> where I supposed it would be of IEnumerable<Type> . Also, there is a typeInfo.AsType() method. What's the meaning of that? are they interchangeable? Moreover, they have

Most efficient way to get default constructor of a Type

依然范特西╮ 提交于 2019-12-31 10:57:16
问题 What is the most efficient way to get the default constructor (i.e. instance constructor with no parameters) of a System.Type? I was thinking something along the lines of the code below but it seems like there should be a simplier more efficient way to do it. Type type = typeof(FooBar) BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance; type.GetConstructors(flags) .Where(constructor => constructor.GetParameters().Length == 0) .First(); 回答1: type