reflection

C# Reflection Invoke - Object of Type 'XXX' Cannot Be Converted to type 'System.Object[]'

不问归期 提交于 2019-12-25 02:44:08
问题 I have created an instance called input that is of type: public class TestInput { public int TesTInt { get; set; } } I use this in this function: public static class TestClass { public static string TestFunction() { var testInput = new TestInput(); string res = ServicesManager.Execute<string>((object) testInput); return res; } } The Execute function is here: public static OUT Execute<OUT>(object input) where OUT : class { var method = //getting method by reflection object[] arr = new object[]

Use reflection to execute a method with parameters raising Exception in GetParameters

亡梦爱人 提交于 2019-12-25 02:26:46
问题 I have a console app that I'm testing the basic ability to enter a command such as: Edit AddUser id=1 name=John surname=Doe Edit The hypothetical method AddUser could look like this: public AddUser(int id, string name, string surname { //add code here to add user } Here's my code: protected void Process(string command) { //get the type Type type = this.GetType(); //do a split on the command entered var commandStructure = command.Split(' '); try { //reassign the command as only the method name

Property name as variable

喜你入骨 提交于 2019-12-25 02:16:46
问题 I need to find the biggest size of each string column in a database as one of the informations to design another database. The only access I have to the source database is by a web service. I can just do it for each of the many columns to find the biggest size but I want it generic so I can use it later. I wrote this very simplified version to make it simple to understand. Two of the last lines have invented syntax and it is where I need help. using System; using System.Collections.Generic;

Property / field initializers in code generation

喜夏-厌秋 提交于 2019-12-25 02:16:14
问题 I'm generating code in a visual studio extension using CodeDom and plain code strings. My extension reads a current classes declared fields and properties using reflection and generates contructors, initializers, implements certain interfaces, etc. The generator class is simple: public class CodeGenerator < T > { public string GetCode () { string code = ""; T type = typeof(T); List < PropertyInfo > properties = t.GetProperties(); foreach (PropertyInfo property in properties) code += "this." +

WinForms/.NET: How to get the event handler attached to an event?

和自甴很熟 提交于 2019-12-25 02:09:53
问题 How can i get the event handler attached to an event? pseudocode, for example's sake: public static EventHandler GetChangeUICuesEventHandler(Control SomeControl) { EventHandler changeUICuesEventHandler = SomeControl.ChangeUICues; return changeUICuesEventHandler } Sample usage (pseudo-code): Control control = GetControlByName(Console.ReadLine()); button1.ChangeUICues += GetChangeUICuesEventHandler(control); 回答1: I offer up the following as an alternative path rather than a direct answer... So

Generic as method return type

梦想与她 提交于 2019-12-25 02:09:42
问题 I have looked around on StackOverflow to find the answer for the problem I am facing. I came across many good answers but still it doesn't answer my question. Get type of a generic parameter in Java with reflection How to find the parameterized type of the return type through inspection? Java generics: get class of generic method's return type http://qussay.com/2013/09/28/handling-java-generic-types-with-reflection/ http://gafter.blogspot.com/search?q=super+type+token So here is what I want

How to reference a strongly-named DLL at compile time, or for ASP.NET view compilation?

我怕爱的太早我们不能终老 提交于 2019-12-25 01:44:43
问题 I have an ASP.NET 4.7.2 Framework project. At runtime, upon loading a view, it says System.Runtime version 4.0.0.0 cannot be found. We have written about this extensively here: ASP.NET: .NET Framework/Standard/Core DLL collisions, caused by Nuget. "You must add a reference to assembly System.Runtime..." It's been over a week and we still don't have a solution. All development has been halted. Yesterday we tried side-by-side loading by simply calling on application start up: Assembly

Java : logging execution history as XML

徘徊边缘 提交于 2019-12-25 01:36:50
问题 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 idea</call> </call> Because the class is long and has lots of methods, I wondered if there is a way to access the parameters generically, maybe using reflection. I am

gRPC and ASP.NET Core: serverside reflection

北战南征 提交于 2019-12-25 01:12:00
问题 I've been trying to implement the concepts of serverside reflection described in https://github.com/grpc/grpc/blob/master/doc/server-reflection.md and for C# https://github.com/grpc/grpc/blob/master/doc/csharp/server_reflection.md in the implementation of ASP.NET Core. You have to somehow configure Kestrel to enable Reflection first. I just don't succeed and have to give up. This is the tricky part that somehow has to be implemented in ASP.NET Core. // the reflection service will be aware of

How to get annotation values in java reflection

独自空忆成欢 提交于 2019-12-25 01:04:51
问题 I have class Person: @Retention(RetentionPolicy.RUNTIME) @interface MaxLength { int length(); } @Retention(RetentionPolicy.RUNTIME) @interface NotNull { } public class Person { private int age; private String name; public Person(int age, String name) { this.age = age; this.name = name; } @NotNull public int getAge() { return this.age; } @MaxLength(length = 3) public String getName() { return this.name; } } Then I'm trying to print annotation values of methods of Peson object. for (Method