reflection

For … in not yielding methods

99封情书 提交于 2021-01-28 10:17:01
问题 Given the following: export class MyClass { public dataA = 0 private dataB = 123 public myMethod(): any { return { test: 'true' } } constructor() { for (const propOrMethod in this) { console.log({propOrMethod}) } } } const myInst = new MyClass() I run this with ts-node index.ts and all i get is: { propOrMethod: 'dataA' } { propOrMethod: 'dataB' } With no reference to myMethod . I would like to iterate over all the methods of my class, but they don't appear to exist 回答1: for..in iterates over

For … in not yielding methods

北城余情 提交于 2021-01-28 10:13:32
问题 Given the following: export class MyClass { public dataA = 0 private dataB = 123 public myMethod(): any { return { test: 'true' } } constructor() { for (const propOrMethod in this) { console.log({propOrMethod}) } } } const myInst = new MyClass() I run this with ts-node index.ts and all i get is: { propOrMethod: 'dataA' } { propOrMethod: 'dataB' } With no reference to myMethod . I would like to iterate over all the methods of my class, but they don't appear to exist 回答1: for..in iterates over

how to append nil to dynamic type slice by reflect.Append

我是研究僧i 提交于 2021-01-28 10:03:53
问题 Code below will raise a runtime error when append reflect.Value of nil : package main import ( "fmt" "reflect" ) func main() { var list []interface{} v := reflect.ValueOf(list) v = reflect.Append(v, reflect.ValueOf(1)) // [1] v = reflect.Append(v, reflect.ValueOf("1")) // [1, 1] v = reflect.Append(v, reflect.ValueOf(nil)) // runtime error fmt.Println(v) } So why there is a runtime error? how can I use reflect.Append to add a nil to interface{} slice? 回答1: interface{} is an interface type, and

how to append nil to dynamic type slice by reflect.Append

喜欢而已 提交于 2021-01-28 10:02:30
问题 Code below will raise a runtime error when append reflect.Value of nil : package main import ( "fmt" "reflect" ) func main() { var list []interface{} v := reflect.ValueOf(list) v = reflect.Append(v, reflect.ValueOf(1)) // [1] v = reflect.Append(v, reflect.ValueOf("1")) // [1, 1] v = reflect.Append(v, reflect.ValueOf(nil)) // runtime error fmt.Println(v) } So why there is a runtime error? how can I use reflect.Append to add a nil to interface{} slice? 回答1: interface{} is an interface type, and

C# Using Reflection list only top classes excluding linked ones

橙三吉。 提交于 2021-01-28 09:33:44
问题 I have a rather simple collection of many (too many..) classes in a specific namespace. They have no nested classes, but the do have linked ones. Example (simplified): namespace ConsoleApp1.Methods { public class Method100Response201 { public Method100Response201() { super = new Method100Response201_1(); } public string aName { get; set; } public string R1_201 { get; set; } = "R1_201"; public Method100Response201_1 super { get; set; } public void DoSpecialThing() { Console.WriteLine (

How get private properties of Class/BaseClass?

别说谁变了你拦得住时间么 提交于 2021-01-28 09:16:59
问题 I use this code: BindingFlags flags= BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public; PropertyInfo prop = myObj.GetProperty("Age", flags); prop is not null. However, when I try to get all properties from myObj : foreach(MemberInfo e in myObj.GetType().GetMembers( flags) ) { //neither GetProperties helps Console.WriteLine(e.Name); } that property ( Age ) is not listed. I can't understand how this happens. 回答1: The dfiference between Type.GetProperty and Type.GetMembers is

How get private properties of Class/BaseClass?

我的未来我决定 提交于 2021-01-28 09:10:15
问题 I use this code: BindingFlags flags= BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public; PropertyInfo prop = myObj.GetProperty("Age", flags); prop is not null. However, when I try to get all properties from myObj : foreach(MemberInfo e in myObj.GetType().GetMembers( flags) ) { //neither GetProperties helps Console.WriteLine(e.Name); } that property ( Age ) is not listed. I can't understand how this happens. 回答1: The dfiference between Type.GetProperty and Type.GetMembers is

How i can change method annotations value in RunTime?

末鹿安然 提交于 2021-01-28 07:50:25
问题 I have controller like @MessageMapping("/room.register") @SendTo("#{sendTo}") public Message addUser(@Payload Message message, SimpMessageHeaderAccessor headerAccessor) { headerAccessor.getSessionAttributes().put("username", message.getSender()); return message; } And i want to change value of SendTo annotation in runtime. I tried to do it as follows: @Aspect public class SendToAspect { @Autowired private WebSocketConfigurationProperties webSocketConfigurationProperties; @Around("execution

Add Event Handler using Reflection ? / Get object of type?

我与影子孤独终老i 提交于 2021-01-28 07:27:47
问题 how to add a event handler myhandler using reflection if I only have the type AType of the event thrower? Delegate myhandler = SomeHandler; EventInfo info= AType.GetEvent("CollectionChanged"); info.AddEventHandler( ObjectOfAType, myhandler ) 回答1: Basically, what you have is fine. The only glitch is that myhandler actually needs to be of the correct type, which is to say: of the type defined by the event. For example: using System; using System.Collections.Specialized; using System.Reflection;

GetField unable to obtain EventClick

别等时光非礼了梦想. 提交于 2021-01-28 07:12:12
问题 I am trying to determine the method that Click event is subscribed to, in my form, and I follow the guide here. Whereas the forum post above able to get the list that the Click event is subscribing to by following code hasClickEventHandler = HasEventHandler(buttonControl, "EventClick"); Assert.AreEqual(hasClickEventHandler, true); private bool HasEventHandler(Control control, string eventName) { EventHandlerList events = (EventHandlerList) typeof(Component) .GetProperty("Events", BindingFlags