reflection

Reflection and generic types

I'm writing some code for a class constructor which loops through all the properties of the class and calls a generic static method which populates my class with data from an external API. So I've got this as an example class: public class MyClass{ public string Property1 { get; set; } public int Property2 { get; set; } public boo...

Get the name of the currently executing method in Ruby

So $0 is the env variable for the top level Ruby program ... but is there one for the current method? ...

Retrieving values in reflected types from reflected properties

I need to access some members marked internal that are declared in a third party assembly. I would like to return a value from a particular internal property in a class. Then I'd like to retrieve a value from a property on that returned value. However, these properties return types that are also internal and declared in this third party...

ASP.Net and GetType()

I want to get a type of a "BasePage" object that I am creating. Every Page object is based off BasePage. For instance, I have a Login.aspx and in my code-behind and a class that has a method Display: Display(BasePage page) { ResourceManager manager = new ResourceManager(page.GetType()); } In my project structure I have a default r...

.net reflection and the "params" keyword

In .net, is there a way using reflection to determine if a parameter on a method is marked with the "params" keyword? ...

Invoking a method using reflection on a singleton object

So I have the following: public class Singleton { private Singleton(){} public static readonly Singleton instance = new Singleton(); public string DoSomething(){ ... } public string DoSomethingElse(){ ... } } Using reflection how can I invoke the DoSomething Method? Reason I ask is because I store the method names in XM...

Get the CustomAttributes of a specific member

Hello, Is there any way to get the custom attributes of a specific object I am receiving in a method? I do not want nor can to iterate over Type.GetMembers() and search for my member. I have the object, which is also a member, that has the attribute. How do I get the attribute? class Custom { [Availability] private object MyO...

Java: At runtime, find all classes in app that extend a base class

I want to do something like this: List<Animal> animals = new ArrayList<Animal>(); for( Class c: list_of_all_classes_available_to_my_app() ) if (c is Anamal) animals.add( new c() ); So, I want to look at all of the classes in my application's universe and when I find one that descends from Animal, I want to create a new objec...

Getting the size of a field in bytes with C#

I'm having a class which I want to inspect it's fields, and report eventually how much bytes does each field take. I assume all fields are of types as Int32, byte etc. How can I find out easily how much bytes does the field take? I need something like: Int32 a; //int a_size = a.GetSizeInBytes; // a_size should be 4 ...

Reflecting method's actions in Java

Hey I'd like to know how to - if even possible - reflect what method calls are executed inside the method during execution. I'm especially interested in either external method calls (that is, methods in other classes) or calling some specific method like getDatabaseConnection(). My intention would be to monitor predefined objects' acti...

Unable to cast object of type 'System.Object[]' to 'MyObject[]', what gives?

Scenario: I'm currently writing a layer to abstract 3 similar webservices into one useable class. Each webservice exposes a set of objects that share commonality. I have created a set of intermediary objects which exploit the commonality. However in my layer I need to convert between the web service objects and my objects. I've used re...

How can you get the names of method parameters in C#?

If I have a method such as: public void MyMethod(int arg1, string arg2) How would I go about getting the actual names of the arguments? I can't seem to find anything in the MethodInfo which will actually give me the name of the parameter. I would like to write a method which looks like this: public static string GetParamName(MethodI...

How to get a MethodInfo in the ThreadContext class?

I'm trying this: Type ThreadContextType = typeof(Application).GetNestedType("ThreadContext", System.Reflection.BindingFlags.NonPublic); MethodInfo FDoIdleMi = ThreadContextType.GetMethod("FDoIdle", BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[] { typeof(Int32) }, null); ThreadContextType is ok but FDoIdleMi is nu...

Java array reflection: isArray vs. instanceof

Is there a preference or behavior difference between using: if(obj.getClass().isArray()) {} and if(obj instanceof Object[]) {} ? ...

How to use Reflection to Invoke an Overloaded Method in .NET

Is there a way to Invoke an overloaded method using reflection in .NET (2.0). I have an application that dynamically instantiates classes that have been derived from a common base class. For compatibility purposes, this base class contains 2 methods of the same name, one with parameters, and one without. I need to call the parameterle...

What is the "cost" of reflection?

I am currently in a programming mentality that Reflection is my best friend. I use it a lot for dynamic loading of content that allows "loose implementation" rather than strict interfaces, as well as a lot of custom attributes. The question I have is, what is the "real" cost to using reflection? Is it worth the effort for frequently ...

getting an Int/short/byte structure byte representation with C#

Given a FieldInfo object and an object, I need to get the actual bytes representation of the field. I know that the field is either int,Int32,uint,short etc. How can I get the actual byte representation? BinaryFormatter.Serialize won't help, since it'll give me more information than I need (it also records type name etc.). The Marshal c...

C# Attribute to trigger an event on invoking a method

Is there a way in C# or .NET in general to create an attribute on a method which triggers an event when a method is invoked? Ideally, I would be able to run custom actions before and after the invocation of the method. I mean something like this: [TriggersMyCustomAction()] public void DoSomeStuff() { } I am totally clueless how to do...

Can I use regular expressions to find a method on a class in java?

I know how to find a method in java using a fixed string, someClass.getMethod("foobar", argTypes); but is there a way to use a regular expression rather than a fixed string to find a method on a given class? An example of the usage might be if I wanted to find a method that was called either "foobar" or "fooBar". Using a regular exp...

Is there a way to know, programatically, how much space an object is taking in memory? (.Net 2.0)

I would like to be able to know, in run-time in my code, how much memory a certain object is taking (a Dataset in this case, but i'm looking for a "general" solution). Is this possible through reflection? This is for .Net 2.0. Thanks! ...