reflection

Get a new object instance from a Type in C#

One may not always know the Type of an object at compile-time, but may need to create an instance of the Type. How do you get a new object instance from a Type?...

C# eval equivalent?

I can do an eval("something()"); to execute the code dynamically in JavaScript. Is there a way for me to do the same thing in C#? What I am exactly trying to do is that I have an integer variable (say i) and I have multiple properties by the names: "Property1","Property2","Property3" etc. Now, I want to perform some operations on the " ...

Using C# with OpenOffice through reflection

I'm working on some code to paste into the currently active OpenOffice document directly from C#. I can't include any of the OpenOffice libraries, because we don't want to package them, so we're using reflection to get access to the OpenOffice API. My question involves using a dispatcher through reflection. I can't figure out the corr...

Programatic access to call stack in .net

How can I get programmatic access to the call stack? ...

Anyone know a quick way to get to custom attributes on an enum value?

This is probably best shown with an example. I have an enum with attributes: public enum MyEnum { [CustomInfo("This is a custom attrib")] None = 0, [CustomInfo("This is another attrib")] ValueA, [CustomInfo("This has an extra flag", AllowSomething = true)] ValueB, } I want to get to those attributes from an ...

How to find an implementation of a C# interface in the current assembly with a specific name?

I have an Interface called IStep that can do some computation (See "Execution in the Kingdom of Nouns"). At runtime, I want to select the appropriate implementation by class name. // use like this: IStep step = GetStep(sName); ...

RFC calculation in Java need help with algorithm

The RFC for a Java class is set of all methods that can be invoked in response to a message to an object of the class or by some method in the class. RFC = M + R where M = Number of methods in the class. R = Total number of other methods directly invoked from the M. Thinking C is the .class and J is the .java file of which we need to ...

How costly is Reflection? Really.

I constantly hear how bad reflection is to use. While I generally avoid reflection and rarely find situations where it is impossible to solve my problem without it, I was wondering... For those who have used reflection in applications, have you measured performance hits and, is it really so bad? ...

How do I intercept a method call in C#?

For a given class I would like to have tracing functionality i.e. I would like to log every method call (method signature and actual parameter values) and every method exit (just the method signature). How do I accomplish this assuming that: I don't want to use any 3rd party AOP libraries for C#, I don't want to add duplicate co...

Python super class reflection

If I have Python code class A(): pass class B(): pass class C(A, B): pass and I have class C, is there a way to iterate through it's super classed (A and B)? Something like pseudocode: >>> magicGetSuperClasses(C) (<type 'A'>, <type 'B'>) One solution seems to be inspect module and getclasstree function. def magicGetSup...

Getting all types that implement an interface with C# 3.5

How can I do what's in the title, with the minimum amount of code, using whatever c# 3.5 syntax (I'm guessing lambda expressions would fit, but I still don't understand them fully)? In short, I want to iterate through all types that implement a particular interface. Edit: I wasn't clear. Let me re-phrase my question: How can I, using r...

Compact Framework - how do I dynamically create type with no default constructor?

I'm using the .NET CF 3.5. The type I want to create does not have a default constructor so I want to pass a string to an overloaded constructor. How do I do this? Code: Assembly a = Assembly.LoadFrom("my.dll"); Type t = a.GetType("type info here"); // All ok so far, assembly loads and I can get my type string s = "Pass me to the cons...

Accessing a Component on an inherited form from the base form

A number of forms in my project inherit from a base form. It is easy to get at the Controls collection of the derived forms, but I have not found a simple way to access the Components collection, since VS marks this as private. I assume this could be done with reflection, but I'm not really sure how best to go about it, not having work...

How to properly cast objects created through reflection

I'm trying to wrap my head around reflection, so I decided to add plugin capability to a program that I'm writing. The only way to understand a concept is to get your fingers dirty and write the code, so I went the route of creating a simple interface library consisting of the IPlugin and IHost interfaces, a plugin implementation library...

VB.NET Get underlying system.type from nullable type

I'm attempting to create a dataset based on the properties of an object. For example, I have an instance of a Person class with properties including ID, Forename, Surname, DOB etc. Using reflection, I'm adding columns to a new dataset based on the object properties: For Each pi As PropertyInfo In person.GetType().GetProperties() Dim...

What is reflection, and why is it useful?

What is reflection, and why is it useful? I'm particularly interested in Java, but I assume the principles are the same in any language(?). ...

Using attributes to cut down on enum to enum mapping and enum/const to action switch statments

I imagine everyone has seen code like: public void Server2ClientEnumConvert( ServerEnum server) { switch(server) { case ServerEnum.One: return ClientEnum.ABC //And so on. Instead of this badness we could do somthing like: public enum ServerEnum { [Enum2Enum(ClientEnum.ABC)] One, } Now we c...

Whats the difference between RuntimeTypeHandle and Type?

And why would I use one over the other in my code? ...

How can I add reflection to a C++ application?

I'd like to be able to introspect a C++ class for its name, contents (i.e. members and their types) etc. I'm talking native C++ here, not managed C++, which has reflection. I realise C++ supplies some limited information using RTTI. Which additional libraries (or other techniques) could supply this information? ...

Can you use reflection to find the name of the currently executing method?

Like the title says: Can reflection give you the name of the currently executing method. I'm inclined to guess not, because of the Heisenberg problem. How do you call a method that will tell you the current method without changing what the current method is? But I'm hoping someone can prove me wrong there. Update: Part 2: Could ...