reflection

How do I get the values of the properties of a model object in ASP.NET MVC when passed in as an object?

泪湿孤枕 提交于 2020-02-25 04:36:10
问题 I would like to be able to write to a log the properties of various objects that I might pass to my LogEvent function. I have various objects defined, such as Town, Taxpayer, TaxedProperty, TaxAccount, etc. in my ASP.NET MVC application. I would like to write to some log passing in my object along with a bunch of other information. I am writing this to a database table and I have a large text field in which I can insert a representation of the object that was being processed when a bad thing

Calling a method of existing object using IL Emit

守給你的承諾、 提交于 2020-02-25 04:16:08
问题 I am attempting to write an attribute based interceptor (something similar to DynamicProxy ). The idea being, based on certain custom attributes, a method inside that attribute would be called, i.e. Call a method inside attribute class before actual method is called. Call the actual method. I am able to override existing method using MethodBuilder and TypeBuilder . However, I can't figure out how to call the method inside the Attribute. My code : static void CreateMethods<T>(TypeBuilder tb) {

Calling a method of existing object using IL Emit

丶灬走出姿态 提交于 2020-02-25 04:15:28
问题 I am attempting to write an attribute based interceptor (something similar to DynamicProxy ). The idea being, based on certain custom attributes, a method inside that attribute would be called, i.e. Call a method inside attribute class before actual method is called. Call the actual method. I am able to override existing method using MethodBuilder and TypeBuilder . However, I can't figure out how to call the method inside the Attribute. My code : static void CreateMethods<T>(TypeBuilder tb) {

C# Dynamic Cast with Reflection and Lists

本小妞迷上赌 提交于 2020-02-24 12:01:06
问题 since yesterday i'm working on a problem and i don't get it yet... I've got a class with many Methods and decide in Runtime wich Method has to be called. Every of this Methods returns a List with Elements from my Businessobjects. My Class looks this way: public class ReflectiveClass { public List<BO1> DoSomethingWithBO1(int param){ List<BO1> list = new List<BO1>(); //.... return list; } public List<BO2> DoSomethingWithBO2(int param){ List<BO2> list = new List<BO2>(); //.... return list; }

caching reflected properties and their custom attributes in c#

99封情书 提交于 2020-02-24 10:49:13
问题 I'm using a custom attribute to grab a property and then set it's value based on another object's value - I'm using reflection to get the attribute like this: Class Property: [MyPropertyName("MyString")] string myString { get; set; } Populating code: public void PopulateMyPropertiesFromObject<T>(MyDataArrays dataArrays, T obj) where T : class, new() { Type type = typeof (T); foreach (PropertyInfo propertyInfo in type.GetProperties()) { foreach (MyPropertyName propName in PropertyInfo

caching reflected properties and their custom attributes in c#

扶醉桌前 提交于 2020-02-24 10:48:47
问题 I'm using a custom attribute to grab a property and then set it's value based on another object's value - I'm using reflection to get the attribute like this: Class Property: [MyPropertyName("MyString")] string myString { get; set; } Populating code: public void PopulateMyPropertiesFromObject<T>(MyDataArrays dataArrays, T obj) where T : class, new() { Type type = typeof (T); foreach (PropertyInfo propertyInfo in type.GetProperties()) { foreach (MyPropertyName propName in PropertyInfo

Scala: Using right type class instance for reflected/runtime instance

落花浮王杯 提交于 2020-02-24 07:17:20
问题 I have a simple trait trait BusinessObject {} and a simple type-class trait Printer[T<:BusinessObject] { def print(instance:T): Unit } In my code base I have a few hundred implementations of BusinessObject . Some are direct implementers, some implement sub-traits of BusinessObject , and some add various mixin traits using with . I have around 10 different special implementations of Printer (defined on the various sub-traits and mixins), and a low-priority generic fallback instance for any

Scala: Using right type class instance for reflected/runtime instance

ぐ巨炮叔叔 提交于 2020-02-24 07:16:46
问题 I have a simple trait trait BusinessObject {} and a simple type-class trait Printer[T<:BusinessObject] { def print(instance:T): Unit } In my code base I have a few hundred implementations of BusinessObject . Some are direct implementers, some implement sub-traits of BusinessObject , and some add various mixin traits using with . I have around 10 different special implementations of Printer (defined on the various sub-traits and mixins), and a low-priority generic fallback instance for any

Linq WHERE EF.Functions.Like - Why direct properties work and reflection does not?

丶灬走出姿态 提交于 2020-02-24 05:29:02
问题 I try to perform a simple LIKE action on the database site, while having query building services based on generic types. I found out while debugging however, that performing EF.Functions.Like() with reflection does not work as expected: The LINQ expression 'where __Functions_0.Like([c].GetType().GetProperty("FirstName").GetValue([c], null).ToString(), "%Test%")' could not be translated and will be evaluated locally. . The code that makes the difference That works : var query = _context.Set

How to convert IList of unknown type (type is known at runtime) to array?

牧云@^-^@ 提交于 2020-02-23 07:24:50
问题 I am using reflection to copy an object of any custom class at runtime. I am using FieldInfo to get all the fields and then properly copy them based on their type. Only type I can work with at the start of the copy algorithm is System.Object (AKA object ). I do a lot of type checking. So when my check method says this particular object is some simple one-dimensional array, it is array, no doubt. However I can access the type of elements in that array only at runtime. I did successfully copied