reflection

Retrieve NHibernate.Collections.Generic.PersistentGenericBag as IList<T> using Reflection

I'm attempting to compare a transient object graph to an NHibernate-persisted object graph. Unfortunately my code breaks where properties of type IList<T> are concerned. The code below works fine with instances of List<T>, because List<T> implements both IList<T> AND IList. Unfortunately, NHibernate's PersistentGenericBag only implements...

Reflection on a Scala case class

I'm trying to write a trait (in Scala 2.8) that can be mixed in to a case class, allowing its fields to be inspected at runtime, for a particular debugging purpose. I want to get them back in the order that they were declared in the source file, and I'd like to omit any other fields inside the case class. For example: trait CaseClassRef...

How to tell if Type A is implicitly convertible to Type B

Given Type a and Type b, how can I, at runtime, determine whether there's an implicit conversion from a to b? If that doesn't make sense, consider the following method: public PropertyInfo GetCompatibleProperty<T>(object instance, string propertyName) { var property = instance.GetType().GetProperty(propertyName); bool isCompatib...

Why does calling a DynamicMethod with an instance of my own class cause an exception?

I'm learning CIL by making my own functions at runtime with Reflection.Emit. I'm actually surprised how easy things have been up until now but I've hit something that I can't guess my way through and I can't find anything relative in the docs. I'm trying to create a function that simply prints a very simple class I have defined. If I ch...

Calling closest fitting method

As part of developing a small ScriptEngine, I reflectively call java methods. A call by the script engine gives me the object the method name and an array of arguments. To call the method I tried to resolve it with a call to Class.getMethod(name, argument types). This however only works when the classes of the arguments and the classes e...

PropertyUtils performance

I have a problem where i need to walk through an object graph and pick out a particular property value. My original solution caches a linked list of property names that need to be applied in order to get from point A to point B in the object graph. I then use apache commons PropertyUtils to iterate through the linked list calling getProp...

How do I sort a generic list based on a custom attribute?

I am working in c#.NEt 2.0. I have a class, let's say X with many properties. Each properties has a custom attribute, an interger, which I planned to use to specify its order int he final array. Using reflection I read through all of the properties and group the values and place them into a generic list of properties. This works and I c...

How to get property.value from reflection.assembly?

How to get property.value from reflection.assembly? Dim assembly As Assembly = assembly.GetExecutingAssembly() For Each assemblyType As Type In assembly.GetTypes() If assemblyType.IsSubclassOf(GetType(Form)) Then 'Dim name As AssemblyName() = assembly.GetReferencedAssemblies() If assemblyType.BaseType....

GetType on a class in a referenced assembly fails

I have an asp.net web project that references a domain project. From within the web project, I want to create an instance of a class from the domain project using reflection, but I always get null (Nothing, in VB). NOTE: I am using the non-fully qualified class name, and was hoping that a search would be performed as MSDN seems to indi...

Are anonymous types in c# accessible through reflection?

As the name of the anonymous type is compiler generated, so is it accessible through reflection? ...

How to get attributes on a referenced field

Hi, I have a question: Is there an elegant way of getting Attributes on a referenced field. Ie.: public class C1: Base { [MyAttribute] public string Field1; } public class Base { private void Do(ref string field) { if (field has attributes) DoSomething(); } } How can I get attributes of a f...

Is there a more efficient way to get an annotated method?

I started a "for fun, nobody knows, nobody cares" open source project (LinkSet). In one place I need to get an annotated method of a class. Is there a more efficient way to do it than this? I mean without the need of iterating through every method? for (final Method method : cls.getDeclaredMethods()) { final HandlerMethod handler...

Can I obtain method parameter name using Java reflection?

If I have a class like this: public class Whatever { public void aMethod(int aParam); } is there any way to know that aMethod uses a parameter named aParam, that is of type int? ...

java: unchecked call to getConstructor(java.lang.Class<?>...)

Hi I am using reflection to construct a class (ConfigBuilder) that takes a File as argument: Class myClassType = Class.forName(className); Class[] types = new Class[] { File.class }; Constructor cons = myClassType.getConstructor(types); Object[] constructorArgs = new Object[] { myFile }; cb = (ConfigBuilder) cons.newInstance(construct...

How to get all types in a referenced assembly?

For whatever reason, I can't seem to get the list of types in a referenced assembly. Not only that, I can't even seem to be able to get to this referenced assembly. I tried AppDomain.CurrentDomain.GetAssemblies(), but it only returns assemblies that have already been loaded into memory. I tried Assembly.GetExecutingAssembly().GetRefer...

How do you access an object whose type is defined at run-time?

I am using FileHelpers to parse CSV files whose structure is unknown using the following code: string cd = string.Format(@"[DelimitedRecord(""{0}"")] public sealed class ImportClass {{ [FieldQuoted('{1}')] ...

Object does not match target type using C# Reflection

Hi everyone! I am trying to get a value of a Window as follows this refers to the main window (window1) Type type = this.GetType(); PropertyInfo pi = type.GetProperty("Left"); object obj = pi.GetValue(type, null); But I get an "Object does not match target type using" error. What is wrong? ...

Reflection in a UserControl to examine containing form. C#

my project is written in c#. I have a usercontrol that I've designed. In that usercontrol I want the ability to look through the form the control is placed on using reflection to look at all other controls on the form. How can I go about doing this? I've used reflection to go through dll's but not for something like this. in summary: M...

c# instantiate class from string

I have an abstract class and I want to initalize it to a class that extends it. I have the child classes name as a string. Besides this... String childClassString; MyAbstractClass myObject; if (childClassString = "myExtenedObjectA") myObject = new ExtenedObjectA(); if (childClassString = "myExtenedObjectB") myObject = new Ext...

What is the design pattern in this code?

Say I have a singleton-ish, factory-ish, reflection-ish class that receives some input, and spits back a new instance of a concrete implementation of some interface. What kind of design is this? Is there a better way to do what I want? Here's some code to illustrate the point: using System; using System.Collections.Generic; // stat...