reflection

How to get the value of a private static field from a class?

試著忘記壹切 提交于 2020-01-10 11:51:13
问题 Is there any way to get value of private static field from known class using reflection? 回答1: Yes. Type type = typeof(TheClass); FieldInfo info = type.GetField(name, BindingFlags.NonPublic | BindingFlags.Static); object value = info.GetValue(null); This is for a field. For a property, change type.GetField to type.GetProperty . You can also access private methods in a similar fashion. 回答2: I suppose someone should ask whether this is a good idea or not? It creates a dependency on the private

Get executing assembly name using reflection

爷,独闯天下 提交于 2020-01-10 03:57:05
问题 I am trying to pull the project name using the reflection, but during the substring method it give me "index out of bound error". string s = System.Reflection.Assembly.GetExecutingAssembly().Location; int idx = s.LastIndexOf(@"\"); s = s.Substring(idx, s.Length); I don't understand why it is giving error on the third line. Plz Help. 回答1: Try: System.IO.Path.GetFileName(System.Reflection.Assembly.GetExecutingAssembly().Location) 回答2: Use the Path class instead of trying to reinvent the wheel

Filtering out auto-generated methods (getter/setter/add/remove/.etc) returned by Type.GetMethods()

一笑奈何 提交于 2020-01-10 03:31:29
问题 I use Type.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic) to retrieve an array of methods for a given type. The problem is the returned MethodInfo could include methods that are generated by the compiler which I don't want. For example: property bool Enabled { get; } will get bool get_Enabled() event SomethingChanged will get add_SomethingChanged(EventHandler) and remove_SomethingChanged(EventHandler) I can probably add some filter logic

SetValue on PropertyInfo instance error “Object does not match target type” c#

旧城冷巷雨未停 提交于 2020-01-10 02:28:19
问题 Been using a Copy method with this code in it in various places in previous projects (to deal with objects that have same named properties but do not derive from a common base class or implement a common interface). New place of work, new codebase - now it's failing at the SetValue with "Object does not match target type" even on very simple examples... and it worked last week.... public static void Copy(object fromObj, object toObj) { Type fromObjectType = fromObj.GetType(); Type

Deep Copy using Reflection in an Extension Method for Silverlight?

霸气de小男生 提交于 2020-01-10 00:27:28
问题 So I'm trying to find a generic extension method that creates a deep copy of an object using reflection, that would work in Silverlight. Deep copy using serialization is not so great in Silverlight, since it runs in partial trust and the BinaryFormatter does not exist. I also know that reflection would be faster then serialization for cloning. It would be nice to have a method that works to copy public, private and protected fields, and is recursive so that it can copy objects in objects, and

How to enumerate passed method parameters

别说谁变了你拦得住时间么 提交于 2020-01-09 19:14:08
问题 One can enumerate the called method parameter types/information like this: private void SomeMethod(int thisValue, string thatValue) { StackTrace stackTrace = new StackTrace(); foreach (ParameterInfo pInfo in stackTrace.GetFrame(0).GetMethod().GetParameters()) { string name = pInfo.Name; string type = pInfo.GetType().ToString(); } } But is there any way to get the actual object of each parameter? EDIT : My goal is to enumerate all parameters and get their values. Using LinQ Expressions, one

How to enumerate passed method parameters

一笑奈何 提交于 2020-01-09 19:13:03
问题 One can enumerate the called method parameter types/information like this: private void SomeMethod(int thisValue, string thatValue) { StackTrace stackTrace = new StackTrace(); foreach (ParameterInfo pInfo in stackTrace.GetFrame(0).GetMethod().GetParameters()) { string name = pInfo.Name; string type = pInfo.GetType().ToString(); } } But is there any way to get the actual object of each parameter? EDIT : My goal is to enumerate all parameters and get their values. Using LinQ Expressions, one

How to call a generic async method using reflection

非 Y 不嫁゛ 提交于 2020-01-09 19:02:25
问题 public interface IBar { } public class Bar : IBar { } public class Bar2 : IBar { } public interface IFoo { Task<T> Get<T>(T o) where T : IBar; } public class Foo : IFoo { public async Task<T> Get<T>(T o) where T : IBar { ... } } I can then call this method using reflection: var method = typeof(IFoo).GetMethod(nameof(IFoo.Get)); var generic = method.MakeGenericMethod(bar2.GetType()); var task = generic.Invoke(foo, new [] { bar2 }); How do I await on this Task ? and How do I cast it to Task

How to call a generic async method using reflection

随声附和 提交于 2020-01-09 19:01:28
问题 public interface IBar { } public class Bar : IBar { } public class Bar2 : IBar { } public interface IFoo { Task<T> Get<T>(T o) where T : IBar; } public class Foo : IFoo { public async Task<T> Get<T>(T o) where T : IBar { ... } } I can then call this method using reflection: var method = typeof(IFoo).GetMethod(nameof(IFoo.Get)); var generic = method.MakeGenericMethod(bar2.GetType()); var task = generic.Invoke(foo, new [] { bar2 }); How do I await on this Task ? and How do I cast it to Task

How to call a generic async method using reflection

空扰寡人 提交于 2020-01-09 19:01:24
问题 public interface IBar { } public class Bar : IBar { } public class Bar2 : IBar { } public interface IFoo { Task<T> Get<T>(T o) where T : IBar; } public class Foo : IFoo { public async Task<T> Get<T>(T o) where T : IBar { ... } } I can then call this method using reflection: var method = typeof(IFoo).GetMethod(nameof(IFoo.Get)); var generic = method.MakeGenericMethod(bar2.GetType()); var task = generic.Invoke(foo, new [] { bar2 }); How do I await on this Task ? and How do I cast it to Task