reflection

How to get current property name via reflection?

I would like to get property name when I'm in it via reflection mechanism. Is it possible? Update: I have code like this: public CarType Car { get { return (Wheel) this["Wheel"];} set { this["Wheel"] = value; } } And because I need more properties like this I would like to do something like this: publ...

Does MethodBase provide file name and line number?

I'm trying to write a CompileTimeValidate(MethodBase method) for postsharp. the problem is when a violation occurs it only shows Description in the Error List. The 'File' and 'Line' columns are empty. The only info that I get to work with is a MethodBase instance of the method that the attribute was applied to. is there a way to get th...

How to get instances in all private fields of an object?

I would like to use reflection to investigate the private fields of an object as well as get the values in those fields but I am having difficult finding the syntax for it. For example, an object has 6 private fields, my assumption is that I could fetch their FieldInfo with something like myObject.GetType().GetFields(BindingFlags.NonPu...

RuntimeType instead of Type in C# Unit Test

Hi, In one of my unit tests I want to check if all public methods are returning type of ActionResult. Here's my test method: [TestMethod] public void Public_Methods_Should_Only_Return_ActionResults() { MethodInfo[] methodInfos = typeof(MyController).GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFl...

MEF: Loading parts (plug-ins) which have differing properties.

Brief Background: My team has decided to use Microsoft's Managed Extensibility Framework (MEF) in order to provide an extensible model for adding new "providers" into our system. This allows for us to plug in new 3rd party providers with relative ease. Note: I was impressed by how simple MEF was to use and get up and running with. My...

how to read the assembly manifest without loading the .dll

Essentially need to read the dependencies programmatically without loading the assembly itself, as then you can't unload them ...

Scala enumeration and reflection

Hello all, After working in Java for a long time, I started to get interested in Scala. As a learning project, I am trying to duplicate a java library that stores and retrieves state objects from the database. For this, I would like to be able to just specify a state object like this: @PersistName("PERSON") case class Person extends ...

How to find the parameterized type of the return type through inspection?

I am using reflection to get all the get all the methods in a class like this: Method[] allMethods = c.getDeclaredMethods(); After that I am iterating through the methods for (Method m: allMethods){ //I want to find out if the return is is a parameterized type or not m.getReturnType(); } For example: if I have a method like...

Why is there not a `fieldof` or `methodof` operator in C#?

They could be used as follows: FieldInfo field = fieldof(string.Empty); MethodInfo method1 = methodof(int.ToString); MethodInfo method2 = methodof(int.ToString(IFormatProvider)); fieldof could be compiled to IL as: ldtoken <field> call FieldInfo.GetFieldFromHandle methodof could be compiled to IL as: ldtoken <method> call MethodBa...

SqlClientPermission failure in assembly loaded via reflection

I am having a really tough time with a problem. I have a web application that I have just modified that allows customers to supply custom assemblies that they can use to hook into an entity save pipeline. These custom assemblies are loaded via reflection when an entity is persisted to the database. They refer to a common DAL assembly ...

Saving a class to a delim file using reflection

I want to write the property names and matching data to a delimited file, I've copied some code from the c# objectdumper help file and it all seems to work OK but I dont understand reflection enough to be confident to use it. What I'm worried about is an incorrect value being placed in the incorrect column, is it possible for this to ha...

C# - Generic CRUD operation

How to achieve generic Save and Update operation using generics and reflection in C#? I have achieved data retrieve using some reference from here... I don't have much time to learn technologies like NHibernate/LINQ/Entity Frameowrk, etc. for this moment. I need a quick solution for this problem for some reason. ...

Why do `Assembly` and `Module` have no publicly defined constructors?

I'm building a .NET assembly loader in C# for an "experiment"/learning more about .NET internal operations. I've implemented the reflection API by deriving types like: RuntimeType : Type RuntimeFieldInfo : FieldInfo RuntimeMethodInfo : MethodInfo RuntimeParameterInfo : ParameterInfo RuntimeConstructorInfo : ConstructorInfo RuntimePrope...

How to return name of the property an unnamed function is assigned to

Hi guys I got following code: function test () { this.testFunction = function () { //code to alert or return the string "testFunction" } } var testVar = new test(); testVar.testFunction(); Is there a way to find out the name of the property, which the unnamed function is assigned to? Whatever I tried yet in conjunction...

How can I retrieve all the KeyValuePairs contained in a Dictonary<?,?> ?

I've got an object which is a Dictionary of an unknown type (ie I don't know the type for the key and the value) I want to retrieve all of its values so I can access those by index. So what I want to do is something like that : Dictionary<object, object> d = (Dictionary<object, object>)obj; // cast error l = new List<KeyValuePair<obje...

Using Activator best way to create instances of classes with multiple constructors?

I have implemented a 'plugin' system where my application creates classes that implement an interface at runtime to allow pluggable functionality. I am achieving this by using Activator.CreateInstance on all classes that implement the specified interface within the plugin assembly. At the current time I am only using one implementatio...

Using generics to process asp.net form request variables into business object

Using jquery to post values back to an asp.net webform. I have some code that works great for sticking posted Form values into my object, basically it converts the form values into the correct type for each property in my class. if (Request.Form.HasKeys()) { foreach (var key in Request.Form.AllKeys) { PropertyInfo itemP...

c# .NET runtime object type

I need to create an instance of an object and the type of that object will be determined at run time. The type of the object is pulled from SQL and set to a string value. I also need to pass a number of parameters when instantiating it. The number/type of parameters will be the same every time (for now at least). What do I need to use to...

Suggestions needed on most efficient way to retrieve an assembly version from an external assembly.

I need to iterate a list of assemblies and determine their version numbers. I've made use of System.Reflection.Assembly.GetExecutingAssembly().GetName().Version before, but in this case I'm working with assemblies that have not yet been loaded. I understand I can use System.Reflection.Assembly.ReflectionOnlyLoadFrom("assembly.dll") but I...

How to mutate a private field from the abstract class in Java?

There's an abstract class: public abstract class AbstractAny { private long period; public void doSomething() { // blah blah blah period = someHardcodedValue; // blah blah blah } } I don't want to change the source of the abstract class but need to add some flexibility on how the field period is being set. Is it pos...