reflection

Get the public properties of a class without creating an instance of it?

南笙酒味 提交于 2021-01-27 14:38:13
问题 Let's imagine that we have a JavaScript class: var Person = (function () { function Person(name, surname) { this.name = name; this.surname = surname; } Person.prototype.saySomething = function (something) { return this.name + " " + this.surname + " says: " + something; }; return Person; })(); I want to iterate its methods and properties. I have no problem with the methods. var proto = Person.prototype, methods = Object.keys(proto); // iterate class methods ["saySomething"] for (var i = 0; i <

How do you call GetMethod for a generic function that takes a generic parameter (without using GetMethods)?

半腔热情 提交于 2021-01-27 13:45:16
问题 I know I can fetch the method info using GetMethods , but I want to know how to do it properly without GetMethods . I have read other SO questions and answers that suggest this is not possible, or suggest just using LINQ instead, but that isn't really an answer to the question. Consider at the most basic level, a static generic function that takes a single generic parameter. private static void Test<T>(T val) { } To fetch this method info we can just call Type.GetMethod("Test", BindingFlags

Checking of variable implements interface without compiling

风流意气都作罢 提交于 2021-01-27 09:00:52
问题 I want to know whether a concrete type implements a specefic interface and print it out. I have written an example [0] with a self defined struct (MyPoint) beeing not an interface-type. MyPoint has the function Read as defined in the interface of io.Reader: type MyPoint struct { X, Y int } func (pnt *MyPoint) Read(p []byte) (n int, err error) { return 42, nil } The aim is to get the information that the concrete type p implements the interface io.Writer. Therefore I have written a short main

F# nameof operator not a first-class function

元气小坏坏 提交于 2021-01-27 07:52:02
问题 I'm using F# 4.7 with <LangVersion>preview</LangVersion> in my project file. I have a type like this: type Record = { Name : string Description : string FieldNotInterestedIn: int } I'd like to get the names of certain fields in a type-safe way, but not all of them. I know I can get all the field names using reflection. Here's the most concise code I came up with. Can it be any more concise? let certainFieldNames = let r = Unchecked.defaultof<Record> [ nameof r.Name nameof r.Description ] 回答1:

F# nameof operator not a first-class function

Deadly 提交于 2021-01-27 07:50:49
问题 I'm using F# 4.7 with <LangVersion>preview</LangVersion> in my project file. I have a type like this: type Record = { Name : string Description : string FieldNotInterestedIn: int } I'd like to get the names of certain fields in a type-safe way, but not all of them. I know I can get all the field names using reflection. Here's the most concise code I came up with. Can it be any more concise? let certainFieldNames = let r = Unchecked.defaultof<Record> [ nameof r.Name nameof r.Description ] 回答1:

How to list (java) annotations on Kotlin data class fields?

前提是你 提交于 2021-01-27 06:31:37
问题 I am using Firestore's Java-based annotation for marking fields and methods for mapping document fields to Java class elements: @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD, ElementType.FIELD}) public @interface PropertyName { String value(); } I am using it on a field in a Kotlin data class, which compiles fine: data class MyDataClass( @PropertyName("z") val x: Int ) In IntelliJ and Android Studio, I can see it show up in the decompiled class dump: public final data class

How to invoke System.Linq.Enumerable.Count<> on IEnumerable<T> using Reflection?

泪湿孤枕 提交于 2021-01-27 04:22:16
问题 I have a bunch of IEnumerable Collections which exact number and types is subject of frequent changes (due to automatic code generation). It looks something like this: public class MyCollections { public System.Collections.Generic.IEnumerable<SomeType> SomeTypeCollection; public System.Collections.Generic.IEnumerable<OtherType> OtherTypeCollection; ... At runtime i want to determine each Type and it's count without having to rewrite the code after every code generation. So i am looking for a

Create a copy of method from IL

老子叫甜甜 提交于 2021-01-27 03:50:39
问题 I am trying to create a copy of a method during runtime using reflection. I have the following code. public static R CopyMethod<T, R>(Func<T, R> f, T t) { AppDomain currentDom = Thread.GetDomain(); AssemblyName asm = new AssemblyName(); asm.Name = "DynamicAssembly"; AssemblyBuilder abl = currentDom.DefineDynamicAssembly(asm, AssemblyBuilderAccess.Run); ModuleBuilder mbl = abl.DefineDynamicModule("Module"); TypeBuilder tbl = mbl.DefineType("Type"); var info = f.GetMethodInfo(); MethodBuilder

Getting collection of all members of a class

独自空忆成欢 提交于 2021-01-21 04:03:17
问题 I want to get the collection of all the members that are present in a class. How do I do that? I am using the following, but it is giving me many extra names along with the members. Type obj = objContactField.GetType(); MemberInfo[] objMember = obj.GetMembers(); String name = objMember[5].Name.ToString(); 回答1: Get a collection of all the properties of a class and their values: class Test { public string Name { get; set; } } Test instance = new Test(); Type type = typeof(Test); Dictionary

Create Func or Action for any method (using reflection in c#)

心已入冬 提交于 2021-01-21 01:37:04
问题 My application works with loading dll's dynamically, based on settings from the database (file, class and method names). To facilitate, expedite and reduce the use of reflection I would like to have a cache.... Following the idea that using: MethodInfo.Invoke Is nothing performative ( Reflection Performance - Create Delegate (Properties C#)) I would like to translate any call to methods. I thought of something that would work like this: public static T Create<T>(Type type, string methodName)