reflection

ICriteria, add restriction on collection contents

Hi all, Using NHibernate I'm trying to get obtain a list of B's where an IList property of B contains a specific instance of A. The following code should hopefully explain the situation more clearly: public void test() { A a1 = new A(); A a2 = new A(); B b1 = new B(); b1.As = new List<A> { a1 }; // ...database save...

Trying to load a list into a DataGridView Cell

I'm trying to display an object into a DataGridView using Reflection so far everything works smootly but the problem is that some of the object's properties are Lists. How can I adapt my DataGridView to show a list? public void SetDataSource(PluginBase plugin) { dgvProperties.Rows.Clear(); List<DataGridViewRow> rows = new List<...

Java: How do I override a method of a class dynamically (class is eventually NOT in classpath)?

How do I call a method of a class dynamically + conditionally? (Class is eventually not in classpath) Let's say, I need the class NimbusLookAndFeel, but on some systems it's not available (i.e. OpenJDK-6). So I must be able to: Get to know it that class is available (at runtime), If it's not the case, skip the whole thing. How do I m...

Where to create classes dynamically? Within Object or global namespace?

I am creating non-namespaced classes dynamically. I have seen an example where this creation is done within the Object class: class Object for elem in ARRAY sub_class = Object.const_set(elem.to_s.camelize, Class.new(SuperClass)) sub_class.class_eval do def initialize(*args, &block) ... super *args, &b...

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

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 (whe...

.NET assemblies: understanding type visibility

I am trying to reproduce something that System.Xml.Serialization already does, but for a different source of data. For now task is limited to deserialization only. I.e. given defined source of data that I know how to read. Write a library that takes a random type, learns about it fields/properties via reflection, then generates and comp...

Passing anonymous type as method parameters

In my plugin architecture I am currently passing a plugin name (string), method name (string) and parameters (object array) to my plugin service to execute the specified method and return the result (of type T). The plugin service's execute method can be seen below: public TResult Execute<TResult>(string pluginName, string operation, p...

GetReferencedAssemblies doesn't return all assemblies.

I have the following code inside a Form, and am attempting to find a way to load a project assembly referenced by the form called DataObjects. Using the following code, I only get six assemblies listed. Looking at the references for the project the form is in, there are thirteen assembly references. What is wrong here? private void L...

How can I get types from a referenced assembly?

I would like to access type information from a referenced (it is a project reference) assembly. The hard way would be to resolve the assembly's file path using VS solution, and load the assembly from file, but I'm sure that since the referenced assembly is already resolved/loaded in the executing assembly, there must be a much easier wa...

Would it theoretically be possible to manipulate a .NET assembly so that everything becomes public?

I don't know much about the CIL, except that there is metadata in assemblies that record the type information. Would it be possible to create some reflecting tool that is able to read a C# or VB.NET assembly and change its metadata so that all class members become public? Would it be straightforward (e.g. change one or two bytes for ea...

Dynamic variables with given type

Hello, I have written a small class, which reads out annotation from methods. Now I want to extend that class to make it more dynamic. My class uses at the moment following code for reading out the annotation: ExtendedCommandAnnotation e = foo.getClass() .getAnnotation(ExtendedCommandAnnotation.class);...

Dumping java.lang.Class's originated from jar files

Hi all, I am trying to find a way to collected all java.lang.Class's loaded from jar files but ignore the ones from the source code itself. I have found java.lang.instrument.Instrumentation interface and thought it might serve the purpose, but it turned out not quite.... One of the available functions "getAllLoadedClasses" dump all j...

Is it possible to pass in a property name as a string and assign a value to it?

I'm setting up a simple helper class to hold some data from a file I'm parsing. The names of the properties match the names of values that I expect to find in the file. I'd like to add a method called AddPropertyValue to my class so that I can assign a value to a property without explicitly calling it by name. The method would look li...

Type from mscorlib not found

There is something I cannot understand. I can't read the type reference: Assembly mscorlib = Assembly.Load("mscorlib"); // it DOES exist, returns type reference: mscorlib.GetType("System.Deployment.Internal.Isolation.IDefinitionAppId"); // but its parent scope doesn't exist.. returns null: mscorlib.GetType("System.Deployment.Internal....

Cannot get PropertyInfo.SetValue() to set the value on my object.

I've simplified the code below down to a basic example, but I still cannot get the value to set. When performing the propertyInfo.SetValue(), it will hit a breakpoint on the setter of my Contact object and the value is set correctly in the 'setter.' However, after the SetValue() has executed, the string properties on my projectContact....

Logging state of object. Getting all its property values as string

public class Address { public string AddressLine1 { get; set; } public string AddressLine2 { get; set; } public string City { get; set; } public string State { get; set; } public string Zip { get; set; } } ...... var emp1Address = new Address(); emp1Address.AddressLine1 = "Microsoft Corporation"; emp1Address.AddressLi...

Using Java reflection to change a Java field's field?

I have a class, server. It has a private field f of type class fg. It in turn has a private field e of type int. I want to modify e, but I can't modify those classes. Succinctly stated, it's: server.(fg)f.(int).e I tried this code, but I'm a bit rusty on my Java, let alone reflection. Line 4 throws a NullPointerException, because it loo...

Looking for a Code Converter to Convert a Normal Java Code to Reflection-Style Code

I'm looking for a converter to make a reflection-style Java code from a normal Java code. I'm doing this to prevent exceptions like NoClassDefFoundError (I want to be dependent to a class, but I want Java to simply ignore the code if the library I'm using doesn't have that dependency class). I expect converter like this: initial code:...

Java Generics Reflection: Generic field type of subclass

Given two classes like this: class Example1<A,B> { public Map<A,B> someMap = new HashMap<A,B>(); } class Example2 extends Example1<URL, URL> { } Is there any way using reflection that I can determine the component types of the Map for Example2? I know I can do: ParameterizedType t = (ParameterizedType) Example2.class.getFie...

How do I check the method state in java?

Lets say I have a instance method named myMethod(). I need to check if any thread is executing the method. Is there a way I can check this using reflection or some other API? I want to execute some cleanup logic when the method is NOT in execution. Hence need to check if the method is running or not. ...