reflection

Fastest way for Get Value of a property (Reflection) in C#

江枫思渺然 提交于 2020-06-25 03:50:33
问题 I want to know what is fastest way to get value (only for this problem) from an object`s property ? after some searching I saw a post from @MarkGravell in this site He wrote this code : using System; using System.Reflection; using System.Reflection.Emit; public class Foo { public Foo(int bar) { Bar = bar; } private int Bar { get; set; } } static class Program { static void Main() { var method = new DynamicMethod("cheat", typeof(int), new[] { typeof(object) }, typeof(Foo), true); var il =

Fastest way for Get Value of a property (Reflection) in C#

拥有回忆 提交于 2020-06-25 03:50:08
问题 I want to know what is fastest way to get value (only for this problem) from an object`s property ? after some searching I saw a post from @MarkGravell in this site He wrote this code : using System; using System.Reflection; using System.Reflection.Emit; public class Foo { public Foo(int bar) { Bar = bar; } private int Bar { get; set; } } static class Program { static void Main() { var method = new DynamicMethod("cheat", typeof(int), new[] { typeof(object) }, typeof(Foo), true); var il =

Swift reflection capabilities - how to get an instance variable name?

五迷三道 提交于 2020-06-16 04:34:39
问题 Given a constructor such as: required init (pTableName : String, pRecordKey : String, pStartAtRecord : Int){ parameters.append(ChildElement(identifier: "pTableName", value: pTableName)) parameters.append(ChildElement(identifier: "pRecordKey", value: pRecordKey)) parameters.append(ChildElement(identifier: "pStartAtRecord", value: pStartAtRecord)) } I want to extract the names of all argument variables such as pTableName, pRecordKey and so forth. Why exactly? First of all, I might have more

C# 8 base interface's default method invocation workaround

泪湿孤枕 提交于 2020-06-10 16:06:09
问题 According to https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-8.0/default-interface-methods It is possible to explicitly invoke an interface base implementation with the following syntax. base(IInterfaceType).Method(); But this doesn't seem to be implemented yet. Is there a workaround (e.g reflection) to achieve this? Example code to illustrate the problem interface IA { void M() { Console.WriteLine("IA.M"); } } interface IB : IA { void IA.M() { Console

C# 8 base interface's default method invocation workaround

旧城冷巷雨未停 提交于 2020-06-10 16:03:29
问题 According to https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-8.0/default-interface-methods It is possible to explicitly invoke an interface base implementation with the following syntax. base(IInterfaceType).Method(); But this doesn't seem to be implemented yet. Is there a workaround (e.g reflection) to achieve this? Example code to illustrate the problem interface IA { void M() { Console.WriteLine("IA.M"); } } interface IB : IA { void IA.M() { Console

Get Reference to Field from Reflection [duplicate]

三世轮回 提交于 2020-06-09 05:38:04
问题 This question already has answers here : How to create a reference to a value-field (4 answers) Closed 16 hours ago . I'm working on an OpenGL game engine as a passion project and im using the UI library "Dear ImGUI" to display and debug values similar to Unity's inspector. I'm having trouble thinking of a way to get a reference to the field I'm trying to debug. Here is the code i have got at the moment but the problem is that its not a reference to the actual field, its just a reference to a

How to get enum Type by specifying its name in String

你离开我真会死。 提交于 2020-06-08 07:53:28
问题 Suppose i have this Enum: namespace BusinessRule { public enum SalaryCriteria : int { [EnumDisplayName(DisplayName = "Per Month")] Per_Month = 1, [EnumDisplayName(DisplayName = "Per Year")] Per_Year = 2, [EnumDisplayName(DisplayName = "Per Week")] Per_Week = 3 } } I have its name in a string variable like : string EnumAtt = "SalaryCriteria"; i am trying to check if this Enum is defined by this name, and if defined i want to get its instance.i have tried like this, but type is returning null :

How to get enum Type by specifying its name in String

天涯浪子 提交于 2020-06-08 07:53:25
问题 Suppose i have this Enum: namespace BusinessRule { public enum SalaryCriteria : int { [EnumDisplayName(DisplayName = "Per Month")] Per_Month = 1, [EnumDisplayName(DisplayName = "Per Year")] Per_Year = 2, [EnumDisplayName(DisplayName = "Per Week")] Per_Week = 3 } } I have its name in a string variable like : string EnumAtt = "SalaryCriteria"; i am trying to check if this Enum is defined by this name, and if defined i want to get its instance.i have tried like this, but type is returning null :

Refer to a property name by variable

让人想犯罪 __ 提交于 2020-06-08 02:39:48
问题 Is there a way to refer to a property name with a variable? Scenario: Object A have public integer property X an Z, so... public void setProperty(int index, int value) { string property = ""; if (index == 1) { // set the property X with 'value' property = "X"; } else { // set the property Z with 'value' property = "Z"; } A.{property} = value; } This is a silly example so please believe, I have an use for this. 回答1: Easy: a.GetType().GetProperty("X").SetValue(a, value); Note that GetProperty(

Refer to a property name by variable

僤鯓⒐⒋嵵緔 提交于 2020-06-08 02:37:07
问题 Is there a way to refer to a property name with a variable? Scenario: Object A have public integer property X an Z, so... public void setProperty(int index, int value) { string property = ""; if (index == 1) { // set the property X with 'value' property = "X"; } else { // set the property Z with 'value' property = "Z"; } A.{property} = value; } This is a silly example so please believe, I have an use for this. 回答1: Easy: a.GetType().GetProperty("X").SetValue(a, value); Note that GetProperty(