reflection

Invoke a method on a .NET object in another arbitrary application

I'm asking mostly out of idle curiosity. When using TestComplete, I've noticed I can point my script at an arbitrary running .NET application, grab a control, reflect on it, and even call methods on it. I have no idea how they pull this off. This isn't simple UIAutomation, as far as I can tell, since I can grab private fields. Also, ...

Ignore missing dependencies during ReflectionOnlyLoad

I am working on a simple class browser dialog that allows users to open an assembly and choose a static method from within. However, there are some situations where the assembly's dependencies are missing. Since I only need the method name and not its full prototype, is there any way to get past the FileNotFoundException that is raised ...

Get value of static field

I've got the following class: public static class Pages { public static string LoggedOut = "LoggedOut.aspx"; public static string Login = "Login.aspx"; public static string Home = "Home.aspx"; } I know I can use Pages.Home statically, but there is a reason for my question. I wish to have a method that I can call like this...

Reflection: How to find from a property info object if that property has a Non Public (Private / Protected) Setter?

Hi Friends, I searched on the forum / Internet for the solution how a PropetryInfo object (of a Public property) can reveal if it has a Private \ Protected Setter ... it was all in vain .... all help I found was about how to "Set" value of a public property having a Private Setter... I would like to know if I have a PropertyInfo objec...

Is it bad to use reflection to simplify constructors, comparisons, etc?

I hate having a bunch of "left/right" methods. Every time a property is added or removed, I have to fix up each method. And the code itself just looks ... wrong. public Foo(Foo other) { this.Bar = other.Bar; this.Baz = other.Baz; this.Lur = other.Lur; this.Qux = other.Qux; this.Xyzzy= other.Xyzzy; } Really this is ...

Custom reflection functionality in .Net

Is it possible to override reflection functionality ? ...

What is the most performant way to log the name of class and method?

I basically want to write a customer logger/tracer which also logs the class and method name of the method that calls the logger/tracer. This should be fast so that it does not affect the performance of the application, strongly typed and clean. Does anyone have good ideas? Here are the few I had but I worry about performance due to ref...

Method may only be called on a Type for which Type.IsGenericParameter is true.

I am getting this error on a routine which uses reflection to dump some object properties, something like the code below. MemberInfo[] members = obj.GetType().GetMembers(BindingFlags.Public | BindingFlags.Instance) ; foreach (MemberInfo m in members) { PropertyInfo p = m as PropertyInfo; if (p != null) { object po = ...

AccessViolationException on MethodInfo.ToString()

Getting an AccessViolationException when calling ToString() on my MemberInfo. I suspect something is holding onto the Type information on one of my methods parameters and causing the error, could this be possible? What usually causes the AccessViolationExpection? I'm getting looping through the Public Static methods a Type and am gettin...

Find names of all classes that a Java program loads using reflection

To support a static-analysis tool I want to instrument or monitor a Java program in such a way that I can determine for every reflective call (like Method.invoke(..)): 1.) which class C this method is invoked on, and 2.) which classloader loaded this class C. Ideally I am looking for a solution that does not require me to statically m...

How to inject C# code at compile-time?

I would like to be able to decorate any method with a custom Trace attribute and some piece of code should be injected into that method at compilation. For example: [Trace] public void TracedMethod(string param1) { //method body } should become: public void TracedMethod(string param1) { Log.Trace("TracedMethod", "param1", par...

C# and Reflection

I'm a brand-newbie to C#, albeit not programming, so please forgive me if I mix things up a bit -- it's entirely unintentional. I've written a fairly simple class called "API" that has several public properties (accessors/mutators). I've also written a testing console application that uses reflection to get an alphabetically list of na...

Reflection Support in C

I know it is not supported, but I am wondering if there are any tricks around it? Any tips? Thank you ...

Correct way to obtain base name of a generic type in .NET is through Substring?

If I have this: Type t = typeof(Dictionary<String, String>); How do I get "System.Collections.Generic.Dictionary" as a string? Is the best/only way to do this: String n = t.FullName.Substring(0, t.FullName.IndexOf("`")); Seems kinda hackish to me though. The reason I want this is that I want to take a Type object, and produce code...

DynamicMethod returns incorrect value when property type is Int64.

I'm working on a routine to use DynamicMethod to retrieve values from a object. It worked fine with most of data types, except for DateTime.Ticks, which is int64 In the following test app. I uses both MethodInfo and DynamicMethod, the methodInfo returns correct value but DynamicMethod doesn't. Any ideas? using System; using System.Re...

Using PropertyInfo.GetValue()

I have a class that creates a static array of all properties, using a static constructor. I also have a function -- GetNamesAndTypes() -- that lists the name & type of each property in that array. Now I want to create another instance-level function -- GetNamesAndTypesAndValues() -- that displays the name & type of each property in the...

Is there a way to get a type's alias through reflection?

I'm writing a simple code generation application to build POCO's from a DB2 database schema. I know it doesn't matter, but I prefer to use type aliases rather than the actual system type name if they are available, i.e., "int" rather than "Int32." Is there a way using reflection that I can get a type's alias rather than it's the actual...

Providing common functionality for aggregates derived from the same base object using the Repository Pattern

I'm attempting to use the Repository Pattern to write a data access layer on an existing DB2 schema. This schema has several aggregates all having a common base entity of a "Document." When building the business objects, I created the Document entity as an abstract, and the aggregates as entities derived from Document. For example: p...

Iterate through Custom Object's Property Names and Values

I'm trying to create an export Excel/CSV function that will iterate through a custom object and first output the property names and then output the values. I want to use reflection only where necessary so I'm attempting to save the property names when I output the headers and then reuse them to print the values. Is this possible? I'm ...

Append an object to an IEnumerable<> via reflection

Hi, I need to be able to access a property via reflection, and, knowing that this property is an IEnumerable, append an object to it. Something like this: Object o; MemberInfo m; Array arr; // Except use IEnumerable, may have to take account of value/ref types arr = (Array)((PropertyInfo)m).GetValue(o, null); } List<o.GetType()> new...