reflection

How do I invoke an extension method using reflection?

I appreciate that similar questions have been asked before, but I am struggling to invoke the Linq Where method in the following code. I am looking to use reflection to dynamically call this method and also dynamically build the delegate (or lambda) used in the Where clause. This is a short code sample that, once working, will help to fo...

System.Security.Permissions.MediaPermission Exception when load image in dll

I'm developing an application using C# in .Net Framework 3.5. In the application I create several objects from different dll (also developed in C#) using reflection. All this objects extend an abstract class (AApplication): private AApplication BuildApplication(string path) { Assembly appAssembly = Assembly.LoadFrom(path...

Best way to call a generic method multiple times (runtime return type) using a parameter-safe invocation

Hi, I feel like I'm so close to working this out and have read dozens of articles and existing questions. I have a method like this: public T DoStuff(object arg1, object arg2) { /* Do stuff and return a T */ } And I need to be able to pass this method to another class for callback purposes. However, when this other class calls bac...

Convert c# by-reference type to the matching non-by-reference type.

I examine the parameters of a C# method using reflection. The method has some out parameters and for these I get back types, which have IsByRef=true. For example if the parameter is declared as "out string xxx", the parameter has type System.String&. Is there a way to convert System.String& back to System.String? The solution should of c...

Built in header file parser in C#?

I was wondering if there was a built in runtime parser for header files in C#. I have several different C header files that I want to parse (They will later be used to determine how a network packet will be deserialized). Ideally, some option to load the .h file dynamically, create the struct, and then use reflection to somehow parse t...

Calling a generic function with a type parameter determined at runtime

Hello, I have a question involving calling a class's generic method with a type parameter that is known at runtime. In specific, the code looks like so: FieldInfo[] dataFields = this.GetType().GetFields( BindingFlags.Public | BindingFlags.Instance ); // data is just a byte array used internally in DataStream DataStream ds = new DataS...

How to obtain a list of constants in a class and their values

I have a class in VB with some constants that represent the string name of my security roles. I need to be able to call a function to return to me a string array(or collection, or whatever) of the values of each of these constants. I will be using it to make sure that my databases Roles table has the same roles as coded into the applic...

C# - Investigating "Method' information using Reflection ?

My intention is to investigate the "Methods" of a Type using reflection in order to verify the following : The Methods should be instance methods and public. Takes the parameter "params" and void in nature. The Method does not makes recursive call. I started as : static void ProcessMethodInfo(Type t) { MethodInfo[] info...

read all classes from java package in classpath

Hi, i need to read classes contained in a java package. Those classes are in classpath. I need to do this task from java program directly. Do you know a simple way to do? List classes = readClassesFrom("my.package") ...

Why use of Reflection in .net C# code are recommended?

Why use of Reflection in .net C# code are recommended? Is it definately a good parctice to use it? What will the possible situations in a project to take maximum benefit of reflection, can we identify some? ...

Java : logging execution history as XML

For debugging purposes, I need to follow the execution of some piece of code, within a class. I would like to generate a log for all method calls, in XML, like : <call class='pack.age.MyClass' method='myMethod1'> <param name='param1'>param1.toString() value</param> ... <call>Call to other method within myMethod1; you get the...

Localization of a Form using ResourceManager

I am currently working on localizing a Form. However I am somewhat confused as to how one correctly does this. I thought it would be possible to export control properties to a resource file automatically, but it seems this is a manual task. My current approach is to add all the Control Properties which are of type String and are writab...

[.Net/Reflection] Getting the .Net corresponding type of a C# type

Hello, is there a function that, given a C# type's string representation, returns the corresponding .Net type or .Net type's string representation; or any way to achieve this. For example : "bool" -> System.Boolean or "System.Boolean" "int" -> System.Int32 or "System.Int32" ... Thanks. Edit : really sorry, it's not a "type to type...

Retrieving a Type's leaf interfaces

The System.Type class provides a GetInterfaces() method that "gets all the interfaces implemented or inherited by the current Type". The problem is that "The GetInterfaces method does not return interfaces in a particular order, such as alphabetical or declaration order. Your code must not depend on the order in which interfaces are ret...

.Assembly / GetExportedTypes throws FileNotFoundException

If I run this code: var myAsm = typeof(MyType).Assembly; var types = myAsm.GetExportedTypes(); I get: System.IO.FileNotFoundException : Could not load file or assembly .... which lists a dependent assembly. However, if I do: var myAsm = Assembly.LoadFrom(...); // DLL containing the same assembly as above var types = myAsm.GetExp...

Calling C# method with parameters from data

Say, I have an XML String like this, <METHOD>foo</METHOD> <PARAM1>abc</PARAM1> <PARAM2>def</PARAM2> ... <PARAM99>ghi</PARAM99> <PARAM100>jkl</PARAM100> and I have a method void foo(String param1, String param2, ..., String param99, String param100) { ... } Is there any easy way for me to map this string to a real method call with t...

Best Practices: What to use Reflection for?

I was toying with the idea of allowing module to with a class in a properties file ; something like availableModules.properties Contact=org.addressbook.ContactMain Business=org.addressbook.BusinessMain Notes=org.addressbook.Notes ... My framework will use reflection to instantiate the relevant modules, and thereafter call methods on...

How do I creating Generic object from dynamically loaded Type?

I'm getting a Type using Assembly class as follows: var asm = Assembly.GetAssembly(typeof(MyAssembly)); var t=asm.GetType("FULLY QUALIFIED CLASS NAME", true, true); Then I create object from this type: var obj = Activator.CreateObject(t, new []{ params }); Now I want to convert or cast this object to a Generic object (actually SubS...

How to I find specific generic overload using reflection?

I am attempting to create an Expression that will invoke a specific generic overloaded method (Enumerable.Average in my first test case). The specific type bindings are not known until runtime however so I need to use Reflection to find and create the correct generic method (the Expression is being created from parsed text). So if I kno...

Using GetCurrentMethod in (supposedly) high-performance code

For logging purposes, some methods in our application include the following line: Dim Log As ILog = GetLog(Reflection.MethodBase.GetCurrentMethod().DeclaringType) I have what might be described as an irrational fear of reflection, which I try to keep in check. However, calls like this in methods that are executed potentially a hundred...