reflection

Testing if an Object is a Dictionary in C#

Is there a way to test if an object is a dictionary? In a method I'm trying to get a value from a selected item in a list box. In some circumstances, the list box might be bound to a dictionary, but this isn't known at compile time. I would like to do something similar to this: if (listBox.ItemsSource is Dictionary<??>) { KeyValu...

Modifying Existing .NET Assemblies

Is there a way to modify existing .NET assemblies without resorting to 3rd party tools? I know that PostSharp makes this possible but I find it incredibly wasteful that the developler of PostSharp basically had to rewrite the functionality of the whole System.Reflection namespace in order to make existing assemblies modifiable. System.R...

Using .Net, how can I determine if a type is a Numeric ValueType?

Title says it all... But here's an example: Dim desiredType as Type if IsNumeric(desiredType) then ... EDIT: I only know the Type, not the Value as a string. Ok, so unfortunately I have to cycle through the TypeCode. But this is a nice way to do it: if ((desiredType.IsArray)) return 0; switch (Type.GetTypeCode(desiredType...

Java equals(): to reflect or not to reflect

This question is specifically related to overriding the equals() method for objects with a large number of fields. First off, let me say that this large object cannot be broken down into multiple components without violating OO principles, so telling me "no class should have more than x fields" won't help. Moving on, the problem came...

Is it possible to define a Java ClassLoader that returns completely different classes to the one's requested?

I've tried this, but get a ClassNotFoundException when calling: Class.forName("com.AClass", false, mySpecialLoader) ...

How to get the source file name and the line number of a type member?

Considering that the debug data file is available (PDB) and by using either System.Reflection or another similar framework such as Mono.Cecil, how to retrieve programmatically the source file name and the line number where a type or a member of a type is declared. For example, let's say you have compiled this file into an assembly: C:\...

How do you determine whether or not a give Type (System.Type) inherits from a specific base class (in .Net)?

This is likely going to be an easy answer and I'm just missing something, but here goes...If I have a Type, (that is, an actual System.Type...not an instance) how do I tell if it inherits from another specific base type? ...

What's the best way to instantiate a generic from its name?

Assuming I have only the class name of a generic as a string in the form of "MyCustomGenericCollection(of MyCustomObjectClass)" and don't know the assembly it comes from, what is the easiest way to create an instance of that object? If it helps, I know that the class implements IMyCustomInterface and is from an assembly loaded into the...

Method 'XYZ' cannot be reflected

We have consumed a third party web service and are trying to invoke it from an ASP.NET web application. However when I instantiate the web service the following System.InvalidOperationException exception is thrown: Method 'ABC.XYZ' can not be reflected. System.InvalidOperationException: Method 'ABC.XYZ' can not be reflected. -...

Obtaining a collection of constructed subclassed types using reflection

I want to create a class which implements IEnumerable<T> but, using reflection, generates T's and returns them via IEnumerable<T>, where T' is a entirely constructed subclass of T with some properties hidden and others read-only. Okay., that might not be very clear. Let me explain this via the medium of code - I'd like to have a clas...

How do I use reflection to invoke a private method in C#?

There are a group of private methods in my class, and I need to call one dynamically based on an input value. Both the invoking code and the target methods are in the same instance. The code looks like this: MethodInfo dynMethod = this.GetType().GetMethod("Draw_" + itemType); dynMethod.Invoke(this, new object[] { methodParams }); In t...

How do I use a common log4net reference in assemblies loaded at runtime?

I have a single-threaded application that loads several assemblies at runtime using the following: objDLL = Assembly.LoadFrom(strDLLs[i]); I would like the assemblies loaded in this manner to use the same log4net.ILog reference as the rest of the assemblies do. But it appears the runtime loaded assemblies have a different reference al...

How to use Java reflection when the enum type is a Class?

I was using an enum in which the constant was a Class. I needed to invoke a method on the constant but could not introduce a compile time dependency and the enum was not always available at runtime (part of optional install). Therefore, I wanted to use reflection. This is easy, but I hadn't used reflection with enums before. The en...

INotifyPropertyChanged property name - hardcode vs reflection?

What is the best way to specify a property name when using INotifyPropertyChanged? Most examples hardcode the property name as an argument on the PropertyChanged Event. I was thinking about using MethodBase.GetCurrentMethod.Name.Substring(4) but am a little uneasy about the reflection overhead. ...

Most efficient way to get default constructor of a Type.

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 | Binding...

A universal reflections API?

Some time back I was working on an algorithm that processed code, and required a reflections API. We were interested in its implementation for multiple languages, but the reflections API for a language would not work for any other language. So is there any thing like a "universal reflections API" that would work for all languages, or may...

Get class property name

I have my winform application gathering data using databinding. Everything looks fine except that I have to link the property with the textedit using a string: Me.TextEdit4.DataBindings.Add(New System.Windows.Forms.Binding("EditValue", Me.MyClassBindingSource, "MyClassProperty", True)) This works fine but if I change the class' pro...

C#: Getting maximum and minimum values of arbitrary properties of all items in a list

I have a specialized list that holds items of type IThing: public class ThingList : IList<IThing> {...} public interface IThing { Decimal Weight { get; set; } Decimal Velocity { get; set; } Decimal Distance { get; set; } Decimal Age { get; set; } Decimal AnotherValue { get; set; } [...even more properties and m...

Advice on C# Expression Trees

I'm working on a method that accepts an expression tree as a parameter, along with a type (or instance) of a class. The basic idea is that this method will add certain things to a collection that will be used for validation. public interface ITestInterface { //Specify stuff here. } private static void DoSomething<T>(Expression<Fun...

How do I invoke a java method when given the method name as a string?

If I have two variables: Object obj; String methodName = "getName"; Without knowing the class of obj, how can I call the method identified by methodName on it? The method being called has no parameters, and a String return value - a getter for a Java bean. ...