reflection

Sorting an array of classes based only on field name

对着背影说爱祢 提交于 2021-01-04 05:42:58
问题 I have an application where a user provides me with the name of a field, e.g name or costInCents , and I have to sort by that field. I have ways of guaranteeing that the field name will be correct. This application causes the complication that I simply cannot make my class Comparable and implement a specific compareTo() , since with a custom implementation of compareTo() I need to know which fields / methods to use at implementation time. So to achieve this goal, I am trying to use reflection

Does CallerMemberNameAttribute use reflection

雨燕双飞 提交于 2021-01-02 05:26:16
问题 You can use the CallerMemberName attribute to avoid specifying the member name as a String argument to the called method when implementing INotifyPropertyChanged interface. The question is does it use reflection behind the scene? Are there any performance hit over hard coding Property name? 回答1: No; the compiler hard-codes the member-name directly during compilation. In terms of the IL, this is ldstr . For example if we compile: static void Implicit() { Log(); } static void Explicit() { Log(

Does CallerMemberNameAttribute use reflection

我只是一个虾纸丫 提交于 2021-01-02 05:26:09
问题 You can use the CallerMemberName attribute to avoid specifying the member name as a String argument to the called method when implementing INotifyPropertyChanged interface. The question is does it use reflection behind the scene? Are there any performance hit over hard coding Property name? 回答1: No; the compiler hard-codes the member-name directly during compilation. In terms of the IL, this is ldstr . For example if we compile: static void Implicit() { Log(); } static void Explicit() { Log(

Appending to go lang slice using reflection

最后都变了- 提交于 2020-12-30 10:45:08
问题 For some reason, it appears that adding new element to slice using reflection doesn't update slice itself. This is the code to demonstrate: package main import ( "fmt" "reflect" ) func appendToSlice(arrPtr interface{}) { valuePtr := reflect.ValueOf(arrPtr) value := valuePtr.Elem() value = reflect.Append(value, reflect.ValueOf(55)) fmt.Println(value.Len()) // prints 1 } func main() { arr := []int{} appendToSlice(&arr) fmt.Println(len(arr)) // prints 0 } Playground link : https://play.golang

Appending to go lang slice using reflection

巧了我就是萌 提交于 2020-12-30 10:41:21
问题 For some reason, it appears that adding new element to slice using reflection doesn't update slice itself. This is the code to demonstrate: package main import ( "fmt" "reflect" ) func appendToSlice(arrPtr interface{}) { valuePtr := reflect.ValueOf(arrPtr) value := valuePtr.Elem() value = reflect.Append(value, reflect.ValueOf(55)) fmt.Println(value.Len()) // prints 1 } func main() { arr := []int{} appendToSlice(&arr) fmt.Println(len(arr)) // prints 0 } Playground link : https://play.golang

How do write a generic function that takes a datarow and fill an object's properties?

天涯浪子 提交于 2020-12-30 01:36:35
问题 I have some function like private static UserInfo FillUserInfoFromDataRow(DataRow dr) { UserInfo user = new UserInfo(); user.UserID = (int) dr["UserID"]; user.UserName = (int) dr["UserName"]; user.ProjectID = (int) dr["ProjectID"]; user.ClassID = (int) dr["ClassID"]; .............. return user; } I'd like to write some generic function like private static T FillEntityInfoFromDataRow(DataRow dr), that will treat analogous types ProjectInfo, JobInfo, etc. I can get all columns names of the

How do write a generic function that takes a datarow and fill an object's properties?

。_饼干妹妹 提交于 2020-12-30 01:31:26
问题 I have some function like private static UserInfo FillUserInfoFromDataRow(DataRow dr) { UserInfo user = new UserInfo(); user.UserID = (int) dr["UserID"]; user.UserName = (int) dr["UserName"]; user.ProjectID = (int) dr["ProjectID"]; user.ClassID = (int) dr["ClassID"]; .............. return user; } I'd like to write some generic function like private static T FillEntityInfoFromDataRow(DataRow dr), that will treat analogous types ProjectInfo, JobInfo, etc. I can get all columns names of the

What is the difference between introspection and reflection?

亡梦爱人 提交于 2020-12-27 07:38:32
问题 Can anyone explain from a language/environment agnostic perspective the difference between these two notions? Also is there a set of conditions that programming languages need to satisfy in order to be reflective and/or introspective? And if there is, what are these conditions? 回答1: The Wikipedia article has a pretty decent summary: In computing, type introspection is the ability of a program to examine the type or properties of an object at runtime. Some programming languages possess this

Dynamically generate expression of property and empty argument

可紊 提交于 2020-12-26 09:05:27
问题 Note: please pay attention carefully this is not a duplicate. I need to create the following Lambda expression: () => model.property the model and its property will be determine at runtime. I want a function that takes the model and property and generate the expression: public object GenerateLambda(object model, string property) { } If it is possible I don't want the function to be generic. but I think the main problem that I have is with () expression. Update : The return type of

Dynamically generate expression of property and empty argument

谁说我不能喝 提交于 2020-12-26 09:05:26
问题 Note: please pay attention carefully this is not a duplicate. I need to create the following Lambda expression: () => model.property the model and its property will be determine at runtime. I want a function that takes the model and property and generate the expression: public object GenerateLambda(object model, string property) { } If it is possible I don't want the function to be generic. but I think the main problem that I have is with () expression. Update : The return type of