reflection

function decorators in c#

Is there a C# analog for Python's function decorators? It feels like it's doable with attributes and the reflection framework, but I don't see a way to replace functions at runtime. Python decorators generally work this way: class decorator(obj): def __init__(self, f): self.f = f def __call__(self, *args, **kwargs): ...

'TypeDescriptor' for Methods - Does it exist?

Hi, I am wondering if there exists a built in class which provides a functionality similar to that of TypeDescriptor, except for method reflection rather than property. I require this to dynamically create a context menu based on flagged methods within an object e.g. [ContextMenuItem(true)] [DisplayName("Do Something")] p...

Java Reflection: Why is it so bad?

I've always avoided Java reflection soley based on its reputation for slowness. I reached a point in the design of my current project where being able to use it would make my code much more readable and elegant, so I decided to give it a go. I was simply astonished by the difference, I noticed at times almost 100x longer run times. Eve...

Looking for a .NET ObjectReflector that will help me simplify reflecting an object

I've got a class that receives an object from other unknown modules and so I have to do reflection on the object to get its data, call its methods, etc. To simplify the code to reflect on the object, I'm building this class: using System; namespace ApplicationCore.Helpers { class ObjectReflector { public object TheObje...

XML Serialization vs Reflection in C#

XML Serialization from MSDN: Serializes and deserializes objects into and from XML documents. The XmlSerializer enables you to control how objects are encoded into XML. Reflection from MSDN Reflection provides objects (of type Type) that encapsulate assemblies, modules and types. You can use reflection to dynamicall...

getting the caller of a method in c#

is there a way to get the instance of class that called some method? ...

Reflection of code

Is it possible to use reflection to know what the code is doing, not the types and methods, but rather what is inside the method (if statements, assignment and so on). I know I can disassemble it, but I need to analyze a class at runtime using C#, for example find out how many if conditions there are... This tool needs to be in C#, any ...

'casting' with reflection

Consider the following sample code: class SampleClass { public long SomeProperty { get; set; } } public void SetValue(SampleClass instance, decimal value) { // value is of type decimal, but is in reality a natural number => cast instance.SomeProperty = (long)value; } Now I need to do something similar through reflection: ...

Test if Convert.ChangeType will work between two types

This is a follow-up to this question about converting values with reflection. Converting an object of a certain type to another type can be done like this: object convertedValue = Convert.ChangeType(value, targetType); Given two Type instances (say FromType and ToType), is there a way to test whether the conversion will succeed? E.g...

Can I change a private readonly inherited field in C# using reflection?

like in java I have: Class.getSuperClass().getDeclaredFields() how I can know and set private field from a superclass? I know this is strongly not recommended, but I am testing my application and I need simulate a wrong situation where the id is correct and the name not. But this Id is private. ...

Reference calling assembly instance in C#

.Net 3.5 sp1 available type question ... Is it possible to "get a handle" or reference to the actual instance of an assembly that called a method? I can get the executing and calling assembly via reflection, but what I'm after is not so much the assembly, but the INSTANCE of that assembly that called method. Simple example (maybe): ...

TypeDescriptor.GetProperties() vs Type.GetProperties()

Consider the following code. Object obj; PropertyDescriptorCollection A = TypeDescriptor.GetProperties(obj); PropertyInfo[] B = obj.GetType().GetProperties(); // EDIT* I'm trying to understand the difference between A and B. From what I understand TypeDescriptor.GetProperties() will return custom TypeDescriptor properties, where a...

Using reflection to set a property of a property of an object

I've got two classes. public class Class1 { public string value {get;set;} } public class Class2 { public Class1 myClass1Object {get;set;} } I've got an object of type Class2. I need to use reflection on Class2 to set the value property... i.e, if I were doing it without reflection, this is how I would go about it: Class2 myOb...

Why are some .Net assemblies not available via an AppDomain's GetAssemblies() method?

I have a little bit of code that loops through the types currently loaded into an AppDomain that runs in an ASP.NET application. Here's how I get the assemblies: var assemblies = AppDomain.CurrentDomain.GetAssemblies(); When the application first starts up there is no problem and all the types I expect are present. But when I update t...

Reflection.Emit Generic Base Class Generic Method Call

I'm dynamically sub classing a generic type (filling it's contract) that has a generic method. I attempt to call this generic method but the assembly I produce has errors. Reflector crashes when trying to open the assembly and this code snippet does not run. The exception I get is: An attempt was made to load a program with an incorrec...

How can I perform a List<object>.Cast<T> using reflection when T is unknown

I've been trying to do this for a good few hours now and this is as far as I have got var castItems = typeof(Enumerable).GetMethod("Cast") .MakeGenericMethod(new Type[] { targetType }) .Invoke(null, new object[] { items }); This returns me System.Linq.Enumerable+d__aa`1[MyObjectType] whereas ...

Is it ok to use reflection to get property names and values in a base class?

I am using the following code to save an object and all its properties to a db. It saves me from having to create a save method in every business object. The code in my base class is: public void Save() { List<SqlParameter> Params = new List<SqlParameter>(); foreach (PropertyInfo Property in this.GetType().GetP...

How to call generic method with a given Type object?

I want to call my generic method with a given type object. void Foo(Type t) { MyGenericMethod<t>(); } obviously doesn't work. How can I make it work? ...

How to write Dynamic VB.net code?

For example i got a class and its got its own properties and i am passing the name of the class and the name of the property to be called to a function Say for example exp is the variable which i am passing which contains a value = "ClassA,Property1" Function Property2BCalled(byval exp as String) dim classname ...

VB.Net how does one call a function name retrieved from a table?

In VB.Net how does one call a Parameterised function name retrieved from a table? My Parameterised Function Name is below procTextToDBTemp(Application.StartupPath & "\" & strFileName) from above function i Stored procTextToDBTemp into the SQL Table How can call that function in the Vb.net How can i do that Please help me Thanx in ...