reflection

.NET Reflection and an array.

I'm trying to get the value of a property that is a single dimensional array via reflection I tried something like this: (try catches removed for clarity) string[] fieldOrder; PropertyInfo fieldOrderColumn; fieldOrderColumn = targetType.GetProperty("OrderArray"); if (fieldOrderColumn == null) throw new Exception(targetType.Name +...

Strongly typed object from string type name (C#)

Hello, if you take a look at the following code, you will (hopefully) see what I am trying to archieve. Basically this code does: A query for generic storag items (they store their type as string) If the item is a subclass of SearchCriteria, create the correct instance Add the instance to the list (SearchCriteria is superclass) Not ...

How do I get the member to which my custom attribute was applied?

I'm creating a custom attribute in C# and I want to do different things based on whether the attribute is applied to a method versus a property. At first I was going to do new StackTrace().GetFrame(1).GetMethod() in my custom attribute constructor to see what method called the attribute constructor, but now I'm unsure what that will giv...

Faster way to cast a Func<T, T2> to Func<T, object> ?

Is there a faster way to cast Fun<TEntity, TId> to Func<TEntity, object> public static class StaticAccessors<TEntity> { public static Func<TEntity, TId> TypedGetPropertyFn<TId>(PropertyInfo pi) { var mi = pi.GetGetMethod(); return (Func<TEntity, TId>)Delegate.CreateDelegate(typeof(Func<TEntity, TId>), mi); } public static Func<...

C#: writing MSIL to add a preprocessor directive

Is it possible in C# to write MSIL code that will add a preprocessor directive to the code, e.g., #warning, if a certain condition is met? Or maybe this can be done with reflection, I don't know. I'm trying to write a custom attribute that, if applied incorrectly to a class's method or property, will generate a compiler warning. Using...

Unexpected Class.getMethod behaviour

A while ago I had a similar question when using Class.getMethod and autoboxing, and it made sense to implement this in your own lookup algorithm. But what really confused me a little was that the following is not working either: public class TestClass { public String doSomething(Serializable s) { return s.toString(); ...

Handling Null in WriteXml

For coonverting Linq to DataTable I am using the following Extension Method(Taken from Stackoverflow) Linq to DataTable public static DataTable ToDataTable<T>(this IEnumerable<T> items) { DataTable table = new DataTable(typeof(T).Name); PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | ...

See if an object is an instance of a class passed through a string

I imagine that there has to be some way to use reflection to do what I want to do. I need to be able to take a string at runtime that is of a certain class, for example: string s = "mypackage.MySuperClass" Then I may have an object of some type. It could be one of the following: mypackage.MySuperClass obj = new mypackage.MySuperClas...

Java reflection: Find fields of a subclass

I have a class hierarchy like so: (=> means "is a subclass of") anonymous instance class => abstract class => generic abstract class or more succinctly: C => B => A When executing, "C" calls one of "A"'s methods. Within that method in "A", I want to use reflection to find protected fields of the object that are defined in class "...

How I can get all reference with Reflection + C#

Hi, Using System.Reflection, I can get all methods from a specific class...but I need know what are the refereces to these methods. For examp. In Visual Studio, if you want the references of specific object...you make right click on the object and select "Find All References"...and Visual Studio show the references of this selected obje...

ICustomTypeDescriptor for simulating strong-typing

I thought about simulating strong-typing for key-value configuration of a new project by providing fake property info via implementing ICustomTypeDescriptor. The configuration instance should provide all default config keys as properties with default values however: I noticed that VS08 intellisense doesn't include "faked" properties whi...

How can I dynamically check the number of arguments expected of an anonymous function in PHP?

Is it possible to get the number of arguments expected of an anonymous function in PHP? I'm aware of ReflectionMethod, but that seems to only work if the method is defined on a class. In my case, the anonymous function is either going to have 1 or two arguments. I'd prefer to do the checking correctly, rather than wrapping the first call...

Is it possible to retrieve all members, including private, from a class in Java using reflection?

I'd like to be able to write, for example Method[] getMethods(Class<?> c) which would do the same thing as the existing Class.getMethods() but also include private and protected methods. Any ideas how I could do this? ...

Is there any way to access the compiler's number for an anonymous class in Java using reflection?

Say I have a Class object which represents an anonymous inner class. Is there any way I can get the compiler's number for the class it created? For example I have a class here whose compilation has resulted in a Thing$1.class file being created. How can I find out this number from the Class object? ...

Using Javassist, how do you determine if a CtField is a collection?

I'm using Javassist in a code generator I'm writing. It's pretty nice, but I've run into a problem. When I'm looking at the CtField in question, I want to determine if it's a collection or not. This is pretty trivial with normal reflection: Collection.class.isAssignableFrom(...) But I haven't quite figured out how to get that same ef...

Implement/instantiate abstract class via reflection in Scala

I'm working on a framework for a EA (evolutionary alg) project in Scala. In this i have a trait that implements common EA-code and leaves problem spesific code like genotype convertion and fitness testing to classes that implement this trait. However, I don't want to fully implement the trait before it is actually run because of testing ...

Reason why MethodBuilder.DefineParameter could not set parameter name?

I'm creating an interface based on an existing interface for WCF concerns, but I have the "DefineParameter" not setting the parameter names (method parameters of the created type have no name). Can you see a reason why? public static Type MakeWcfInterface(Type iService) { AssemblyName assemblyName = new AssemblyName(Stri...

When building a datagrid helper, how do you access the new data annotation attributes using Reflection?

Hi, So I've eagerly added the System.ComponentModel.DataAnnotations namespace to my model. I've added things such as: [Required] [DisplayName("First Name")] public string first_name {get;set;} I really like these attributes because they save me from having to write custom T4 and/or modify views heavily. This way, I can regenerate a...

[java reflection] problem with arguments when running a jar using reflection

Hi all, I try to run a jar using reflection with the the getMethod and invoke method but ran in trouble with the arguments passed to the invoke method: String mainClass = myprog.Run; String[] args = new String[2]; args[0] = "-a"; args[1] = "-c ./config/param.xml" ClassLoader classLoader = createClassLoader(getClassPath()); // Invoke ...

.NET Base Validator and reflection

I am creating .NET validators dynamically and passing the property I am invoking and the value to a method that invokes the property and supplies the value. This is working for most of the properties. But when I try to invoke the Operator method or the Type method of the compare validator, I get an error saying the property cannot be fo...