reflection

Reconstructing generic types at runtime with Guice via Types and TypeLiterals

I have a few types that are like this // a value that is aware of its key type (K) Bar<K> // something that deals with such values and keys Foo<V extends Bar<K>, K> how would one recreate Foo such that you could consume it in guice? the bit I'm stuck on is how to cross reference the K from Bar to the 2nd parameterised type of Foo. s...

Upto What extent Reflection should be used ?

Hi, We've got into a very tricky scenario in a project. We have used lot of reflection in our project. We have .. Validation Framework driven by Attributes and Reflection Extension methods that transaforms a DataRow to an Entity Object using Attributes and Reflection and vice versa. The same thing we have done for DataTable and Enti...

How to dynamically call a class' method in .NET?

Hi, How to pass a class and a method name as strings and invoke that class' method? Like void caller(string myclass, string mymethod){ // call myclass.mymethod(); } Thanks ...

.NET FieldInfo -- get the object of which it *is* a field

How does one obtain programmatically a reference to the object of which a FieldInfo object is a field? For example, I'd like something like this: myFieldInfo.GetOwner(); // returns the object of which myFieldObject is a field ...

Setting/Changing an .NET application's TimeZone

Is there a way to set a .NET application's TimeZone to a value other than the OS's TimeZone? For instance, I am using my application on an OS with Central Standard Time set, and I would like the app to behave as if it were in Eastern Standard Time, without having to manually convert all DateTimes in my application using a method like ...

.NET, C#, Reflection: list the fields of a field that, itself, has fields

In .NET & C#, suppose ClassB has a field that is of type ClassA. One can easily use method GetFields to list ClassB's fields. However, I want to also list the fields of those ClassB fields that themselves have fields. For example, ClassB's field x has fields b, s, and i. I'd like to (programmatically) list those fields (as suggested by ...

Make Object Immutable at Runtime [C#]

Is there any way (utilizing Reflection I hope) that I can make an instantiated object immutable along with all of its public properties? I have a class from someone else's codebase (no source available) that I need to utilize and I basically want an exception to be thrown if any piece of code anywhere tries to call a public setter within...

Reference 'this' in dynamic event handler

In my 'myClass' class, I am using Reflection.Emit to dynamically write an event handler for one of the myClass class' members. I have done this successfully. Now, I want to modify the event handler to call one of the instance methods in the myClass class. However, I cannot figure out how to push a reference to 'this' onto the MSIL sta...

Problem with reflection in Unit/Integration tests

Hi I´m dynamically creating an instance of a class with reflection and this works fine, except when trying to do this through unit testing - I´m using the MS testing framework. I get the familiar error of: "Could not load file or assembly 'Assy' or one of its dependencies. The system cannot find the file specified" I have copied the d...

.NET Reflection: determine sizes of a class' fields

Goal: to programmatically determine the sizes (in bytes) of the fields of a class. For example, see the comments below ... class MyClass { public byte b ; public short s ; public int i ; } class MainClass { public static void Main() { foreach ( FieldInfo fieldInfo in typeof(MyClass).GetFields(B...

Matching ASP.NET source code to a compiled web application

My client has a compiled ASP.NET 2.0 application that was compiled & deployed a year ago. They also have 4 versions of source code projects/solutions not under source control (stored on previous developer's workstation file system). None of the file dates appear to match one another. Is there any way to determine which (if any) of thos...

Finding out if a type implements a generic interface

Let's say I have a type, MyType. I want to do the following: Find out if MyType implements the IList interface, for some T. If the answer to (1) is yes, find out what T is. It seems like the way to do this is GetInterface(), but that only lets you search by a specific name. Is there a way to search for "all interfaces that are of the...

Interpreting Java reflection performance: Why is it surprisingly very fast?

I've seen other threads saying java reflection performance is 10-100x slower than when using non-reflection calls. My tests in 1.6 have shown that this is not the case but I found some other interesting things that I need someone to explain to me. I have objects that implement my interface. I did three things 1) using a reference to a...

How to get System.Type instance of a class-type in Static Member ?

Hi, I have a public static property in a class. The class has some custom attributes applied to it. I want to access the Attribute in a static property. In a non-static member I can get the type of current class using this.GetType() but how do I do this in a static member of the class ? Please note that.. I do not want to use typeof...

Builds a Delegate from MethodInfo ?

After googling and landing on SO and having read this other question Is it possible to build a correct Delegate from a MethodInfo if you didn't know the number or types of parameters at compile time? More on this: can this be done elegantly without the use of Reflection.Emit or type builders? This is sorta a bummer for me because ...

How to add a BackgroundWorker RunWorkerCompleted event through reflection?

Normally I would go: bgwExportGrid.RunWorkerCompleted += ReportManager.RunWorkerCompleted; The ReportManager class is a static class containing the event handler I want to use. public static class ReportManager { public static void RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { ... ...

What should I use instead of LoadWithPartialName()?

I'm loading an assembly with LoadWithPartialName(), but VS tells me that it's obsolete and to use Load() instead. However, I can't find any convenient overload. There is a Load(string) with asks for a "full-name" which, if I understood correctly the MSDN docs, includes things like the version number. There is also a Load (string, Evide...

C#: Retrieving and using an IntPtr* through reflection

I'm currently working on some code which reflects over structures that are marshaled back from calls into a native dll. Some of the structs contain IntPtr* fields that point to null-terminated arrays of pointers. These fields require special processing. When reflecting over the structs, I can recognize these fields because they are ma...

Specifying generic collection type param at runtime (Java Reflection)

Hi, I'd like to get the generic type of a collection, using reflection, at runtime. Code (JAVA): Field collectionObject = object.getClass().getDeclaredField( collectionField.getName()); //here I compare to see if a collection if (Collection.class.isAssignableFrom(collectionObject.getType())) { // here I have to use the generic t...

C# - How to reflect the generic parameter that was used for inheritance

Let's say I have the following class hierarchy: TaskViewer inherits from ListViewer<Task> which in turn inherits from ViewerBase. If I debug into a method that is declared in ViewerBase and look at this.GetType(), it correctly returns TaskViewer. However, I cannot find a property or method which will return me the generic parameter tha...