reflection

.NET 2.0: Invoking Methods Using Reflection And Generics Causes Exception

Hi all, I'm new to Stack Overflow, so forgive me. I've just started transititoning over to C# and I am stuck on a problem. I am wanting to pass a generic class in and call a method from that class. So, my code looks as such: public void UpdateRecords<T>(T sender) where T : new() //where T: new() came from Resharper { Type foo = Ty...

Determine if method is unsafe via reflection

I'm looking for a way to filter out methods which have the unsafe modifier via reflection. It doesn't seem to be a method attribute. Is there a way? EDIT: it seems that this info is not in the metadata, at least I can't see it in the IL. However reflector shows the unsafe modifier in C# view. Any ideas on how it's done? EDIT 2: For my...

Class Property in for each

Hi guys, I am running from a problem while iterating over class properties. I have my first class: class Item private _UIN as integer = 0 private _Name as string = "" private _Category as ItemCategory = new ItemCategory() public Property UIN() as integer public property Name() as string public property Category() as ItemC...

What's the term (if any) for frameworks that support dynamic class creation?

Hi. Sorry about the vocabulary question, but I'm writing my master thesis and it's a pain to repeat "frameworks that support dynamic class creation" again and again. Is there a term for that? Some clarification: I mean that you can create a class at runtime, i.e., dynamically. For example, .NET supports this with the System.Reflection n...

Retrieve enum value based on XmlEnumAttribute name value

I need a Generic function to retrieve the name or value of an enum based on the XmlEnumAttribute "Name" property of the enum. For example I have the following enum defined: Public Enum Currency <XmlEnum("00")> CDN = 1 <XmlEnum("01")> USA= 2 <XmlEnum("02")> EUR= 3 <XmlEnum("03")> JPN= 4 End Enum The first Currency enum val...

PropertyInfo SetValue and nulls

If I have something like: object value = null; Foo foo = new Foo(); PropertyInfo property = Foo.GetProperties().Single(p => p.Name == "IntProperty"); property.SetValue(foo, value, null); Then foo.IntProperty gets set to 0, even though value = null. It appears it's doing somemething like IntProperty = default(typeof(int)). I would l...

How do you do dynamic script evaluation in C#?

What is the state of dynamic code evaluation in C#? For a very advanced feature of an app I'm working on, I'd like the users to be able to enter a line of C# code that should evaluate to a boolean. Something like: DateTime.Now.Hours > 12 && DateTime.Now.Hours < 14 I want to dynamically eval this string and capture the result as a bo...

Java dynamic function calling

I have a String array that contains names of method in the yyyyyy class In the xxxxxx class I'm making a yyyyyy instance (say obj). Now I can call obj.function_name(), except I want to read function_name from the String array in a loop. Is this possible? ...

check properties of two objects for changes

Hi, i have to develop a mechanism to check two object properties for changes. All properties which are needed to check are marked with an attribute. Atm i - read all properties from acutal object via linq - read the corresponding property from old object - fill an own object with the two properties (old and new value) In Code the call...

Merging .net object graph

Hi guys, has anyone come across any scenario wherein you needed to merge one object with another object of same type, merging the complete object graph. for e.g. If i have a person object and one person object is having first name and other the last name, some way to merge both the objects into a single object. public class Person { p...

Invoking static methods containing Generic Parameters using Reflection.

While executing the following code i gets this error "Late bound operations cannot be performed on types or methods for which ContainsGenericParameters is true." class Program { static void Main(string[] args) { MethodInfo MI = typeof(MyClass).GetMethod("TestProc"); MI.MakeGenericMethod(new [] {typeof(string)});...

GridView ObjectDataSource LINQ Paging and Sorting using multiple table query.

I am trying to create a pageing and sorting object data source that before execution returns all results, then sorts on these results before filtering and then using the take and skip methods with the aim of retrieving just a subset of results from the database (saving on database traffic). this is based on the following article: http:/...

Getting fields of a class through reflection

I've done it a gazillion times in the past and successfully so. This time, I'm suffering from lapses of amnesia. So, I am just trying to get the fields on an object. It is an embarrassingly simple and stupid piece of code that I am writing in a test solution before I do something really useful in production code. Strangely, the GetFiel...

How to tell whether a class/method is accessible using reflection?

I use a Dynamic Assembly to create derived classes at run time. How can I tell, using reflection, whether the base class and individual methods in the base class can be used/called from within the derived class in the dynamic assembly? ...

Test if a method is an override?

Possible Duplicate: Detect if a method was overridden using Reflection (C#) Is there a way to tell if a method is an override? For e.g. public class Foo { public virtual void DoSomething() {} public virtual int GimmeIntPleez() { return 0; } } public class BabyFoo: Foo { public override int GimmeIntPleez() { retur...

The uncatchable exception, pt 2

Update: I've filed a bug report on Microsoft Connect: https://connect.microsoft.com/VisualStudio/feedback/details/568271/debugger-halting-on-exception-thrown-inside-methodinfo-invoke#details If you can reproduce this problem on your machine, please upvote the bug so it can be fixed! Ok I've done some testing and I've reduced the prob...

Define a method in base class that returns the name of itself (using reflection) - subclasses inherit this behavior

In C#, using reflection, is it possible to define method in the base class that returns its own name (in the form of a string) and have subclasses inherit this behavior in a polymorphic way? For example: public class Base { public string getClassName() { //using reflection, but I don't want to have to type the word "Bas...

Is there a .NET BCL class to help with hand-rolled property path binding?

WPF and Silverlight have a data binding model whereby I can provide a Binding with a Path which comprises a dot-notation of property accessors down from a DataContext to a specific value inside a complex object graph (eg. MyDataContext.RootProperty.SubProperty.Thing.Value) I have a (non-UI) requirement to accept such a path expressed a...

Get derived class type from a base's class static method

Hi, i would like to get the type of the derived class from a static method of its base class. How can this be accomplished? Thanks! class BaseClass { static void Ping () { Type t = this.GetType(); // should be DerivedClass, but it is not possible with a static method } } class DerivedClass : BaseClass {} // somewhere in the...

Getting the constructor of an Interface Type through reflection, is there a better approach than looping through types?

I have written a generic type: IDirectorySource<T> where T : IDirectoryEntry, which I'm using to manage Active Directory entries through my interfaces objects: IGroup, IOrganizationalUnit, IUser. So that I can write the following: IDirectorySource<IGroup> groups = new DirectorySource<IGroup>(); // Where IGroup implements `IDirectoryEnt...