reflection

Is there a C# equivalent of typeof for properties/methods/members?

A classes Type metadata can be obtained in several ways. Two of them are: var typeInfo = Type.GetType("MyClass") and var typeInfo = typeof(MyClass) The advantage of the second way is that typos will be caught by the compiler, and the IDE can understand what I'm talking about (allowing features like refactoring to work without silentl...

F# equivalent of the C# typeof(IEnumerable<>)

I have a piece of code where I need to figure out if a given type implements IEnumerable (I don't care about the T) I've tried (t:System.Type in case you wonder) let interfaces = t.GetInterfaces() let enumerbale = interfaces.Any( fun t -> (t.GetGenericTypeDefinition() = typeof<IEnumerable<>>) however that wont c...

Compute hash of class code at runtime (C#)?

Is it possible to compute the hash of a class at runtime in C# (presumably through reflection)? To be clear, I don't want to compute the hashcode of an instance of said class, I want to compute the hash of the class code itself (if a function in the class changes, I'd expect a different hash code to be computed). Ideally this would only ...

Getting all types from an assembly derived from a base class

I am trying to examine the contents of an assembly and find all classes in it that are directly or indirectly derived from Windows.Forms.UserControl. I am doing this: Assembly dll = Assembly.LoadFrom(filename); var types = dll.GetTypes().Where(x => x.BaseType == typeof(UserControl)); But it is giving an empty list because none of the...

Suggestions on Working with this Inherited Generic Method

We have inherited a project that is a wrapper around a section of the core business model. There is one method that takes a generic, finds items matching that type from a member and then returns a list of that type. public List<T> GetFoos<T>() { List<IFoo> matches = Foos.FindAll( f => f.GetType() == typeof(T) ); L...

How to limit setAccessible to only "legitimate" uses?

The more I learned about the power of java.lang.reflect.AccessibleObject.setAccessible, the more astonished I am at what it can do. This is adapted from my answer to the question (Using reflection to change static final File.separatorChar for unit testing). import java.lang.reflect.*; public class EverythingIsTrue { static void setF...

Field.SetValue alternative

Is there any. I just ran the new perf analysis tool of vs2010 and as it turns out this func consumes most of the cpu resources. Is there something else i can use? ...

How To Test if a Type is Anonymous?

I have the following method which serialises an object to a HTML tag. I only want to do this though if the type isn't Anonymous. private void MergeTypeDataToTag(object typeData) { if (typeData != null) { Type elementType = typeData.GetType(); if (/* elementType != AnonymousType */) { _tag.Att...

Help me find some good 'Reflection' reading materials??

If it wasn't you guys I would've never discovered Albahari.com's free threading ebook. My apologies if I come off as being extremely lazy but I need to make efficient use of my time and make sure am not just swallowing any garbage of the web. Therefore I am looking for the best and most informative and with a fair bit of detail for the ...

Associate "Code/Properties/Stuff" with Fields in C# without reflection. I am too indoctrinated by Javascript

I am building a library to automatically create forms for Objects in the project that I am working on. The codebase is in C#, and essentially we have a HUGE number of different objects to store information about different things. If I send these objects to the client side as JSON, it is easy enough to programatically inspect them to ge...

Reflection alternative for switch on enum in order to select namespace/class

Hi, I have an interface named IHarvester. There are 3 implementations of that interface, each under their own namespace: Google Yahoo Bing A HarvesterManager uses the given harvester. It knows the interface and all 3 implementations. I want some way of letting the class user say in which harvester it wants to use. And in the code ...

What is the use of reflection in Java/C# etc

I was just curious, why should we use reflection in the first place? // Without reflection Foo foo = new Foo(); foo.hello(); // With reflection Class cls = Class.forName("Foo"); Object foo = cls.newInstance(); Method method = cls.getMethod("hello", null); method.invoke(foo, null); We can simply create an object and call the class's m...

What java apis are there to help with annotation processing by using reflection?

Hi, I`m looking to start project that needs to process annotations. I was wondering what apis are available to help with reflection and annotation processing. I know theres Apache BeanUtils but are there any others? ...

CreateDelegate with unknown types

Hello, I am trying to create Delegate for reading/writing properties of unknown type of class at runtime. I have a generic class Main<T> and a method which looks like this: Delegate.CreateDelegate(typeof(Func<T, object>), get) where get is a MethodInfo of the property that should be read. The problem is that when the property return...

Activator.CreateInstance fails in Windows Service

I have an issue with a Windows Service which throws a NullReference exception whenever I try to use var myType = Activator.CreateInstance(typeof(MyType)) There is no problem whenever I run the exact same code in a console window - and after debugging the obvious "which user is running this code" I doubt that it is a mere fact of chan...

Identify in a unit test if Jetbrains IntelliJ IDEA 8 or 9 is running

I need to know, in a context of a unit test, if Jetbrains IntelliJ idea is the test initiator and if it is, which version is running (or at least if it's "9" or earlier). I don't mind doing dirty tricks (reflection, filesystem, environment...) but I do need this to work "out-of-the-box" (without each developer having to setup something ...

XmlSerializer throws exception when serializing dynamically loaded type

Hi I'm trying to use the System.Xml.Serialization.XmlSerializer to serialize a dynamically loaded (and compiled class). If I build the class in question into the main assembly, everything works as expected. But if I compile and load the class from an dynamically loaded assembly, the XmlSerializer throws an exception. What am I doing wr...

How do you Read the value of an attribute in a Method

I need to be able to read the value of my attribute from within my Method, how can I do it? [MyAttribute("Hello World")] public int MyMethod() { //Need to read the MyAttribute attribute and get its value } ...

C# - Cast object to IList<T> based on Type

I am trying out a little reflection and have a question on how the cast the result object to an IList. Here is the reflection: private void LoadBars(Type barType) { // foo has a method that returns bars Type foo = typeof(Foo); MethodInfo method = foo.GetMethod("GetBars") .MakeGenericMethod(bar); object obj = m...

Junit: Test Spring (auto)wiring with reflection?

Hi, Is it possible to JUnit test if wiring by Spring is succesfully? I would like to do this by reflection. Like: get all beans with id *Controller and test if the fields *services are not null? Thank you! ...