reflection

Error binding to target method

无人久伴 提交于 2020-01-15 09:13:33
问题 I am trying to call a static method from a helper class, of which the type is not known until runtime. I thought I had solved the problem however I am getting the following error - "Error binding to target method." Can anyone see what is wrong with this code? Any help would be appreciated.. Delegate del = Delegate.CreateDelegate(typeof(Func<string>), typeof(RepositoryStringExtensions).GetMethod("GetTableName", BindingFlags.Static | BindingFlags.Public) .MakeGenericMethod(new Type[] {

Loading a ParameterInfo using IL Emit

微笑、不失礼 提交于 2020-01-15 08:55:12
问题 I am currently using Calling a method of existing object using IL Emit as a guide, and I can already do whatever is asked in question. Now, I have an attribute added to a parameter and I want to load that particular parameter's attribute so that I can call a method inside that attribute. I know it can be done by loading the MethodInfo and then getting the ParameterInfo and then getting attribute of that ParameterInfo in IL; I am simply trying to avoid writing that much IL. Is there a way to

How to redirect a method call?

点点圈 提交于 2020-01-15 05:15:19
问题 I have got a DLL which works fine in Windows, but Inside one of its private functions the static System.IO.File.ReadAllBytes() is called. I have to use this DLL on a windows CE smart Device Project with Compact Framework 3.5 and there is no such method in the System.IO.File. I have tried to create a class named "File" inside the project like this: public class File { internal static byte[] ReadAllBytes(string path) { } internal static void WriteAllBytes(string path, byte[] bytes) { } } My own

How can get all non-static members of a type?

天涯浪子 提交于 2020-01-15 05:01:09
问题 __traits(allMembers, T) returns both instance and static members. How can I filter out the static members? I'd like this to work for both fields and methods. 回答1: Sure you can do this. D's introspection power is immense, in your case Filter from std.meta is your friend ;-) struct Lion { static maxSpeed = 100; string name; bool isDangerous() { return true; } static bool isAlive(uint meat) { return meat > 100; } } template FilterMembers(alias T, bool filterStatic = true) { import std.meta :

Copy a Nullable property to a non-Nullable version using Reflection

别来无恙 提交于 2020-01-15 03:49:09
问题 I am writing code to transform one object to another using reflection... It's in progress but I think it would boil down to the following where we trust both properties have the same type: private void CopyPropertyValue(object source, string sourcePropertyName, object target, string targetPropertyName) { PropertyInfo sourceProperty = source.GetType().GetProperty(sourcePropertyName); PropertyInfo targetProperty = target.GetType().GetProperty(targetPropertyName); targetProperty.SetValue(target,

Setting/Changing an .NET application's TimeZone

回眸只為那壹抹淺笑 提交于 2020-01-14 19:22:28
问题 Is there a way to set a .NET application's TimeZone to a value other than the OS's TimeZone? For instance, I am using my application on an OS with Central Standard Time set, and I would like the app to behave as if it were in Eastern Standard Time, without having to manually convert all DateTimes in my application using a method like TimeZoneInfo.ConvertTimeBySystemTimeZoneId() . 回答1: No, I don't believe there's any way of doing this. It's not like the time zone is set on a per thread basis

How to find types that are direct descendants of a base class?

落爺英雄遲暮 提交于 2020-01-14 18:44:12
问题 I need to get all types of an assembly that inherits some base class but only the first descendants. For instance if I have: class Base { } class FirstClass : Base { } class SecondClass : FirstClass { } Now var directOnes = assembly.GetTypes().Where(t => t.IsSubclassOf(typeof(Base))); should return only FirstClass and not SecondClass . Is there a way to find out? 回答1: Instead of IsSubclassOf() you can use Type.BaseType e.g. var directOnes = assembly.GetTypes().Where(t => t.BaseType == (typeof

Reflection and Generics get value of property

社会主义新天地 提交于 2020-01-14 17:51:10
问题 i am trying to implement a generic method which allows to output csv file public static void WriteToCsv<T>(List<T> list) where T : new() { const string attachment = "attachment; filename=PersonList.csv"; HttpContext.Current.Response.Clear(); HttpContext.Current.Response.ClearHeaders(); HttpContext.Current.Response.ClearContent(); HttpContext.Current.Response.AddHeader("content-disposition", attachment); HttpContext.Current.Response.ContentType = "text/csv"; HttpContext.Current.Response

Dynamic swappable Data Access Layer

匆匆过客 提交于 2020-01-14 14:41:07
问题 I'm writing a data driven WPF client. The client will typically pull data from a WCF service, which queries a SQL db, but I'd like the option to pull the data directly from SQL or other arbitrary data sources. I've come up with this design and would like to hear your opinion on whether it is the best design. First, we have some data object we'd like to extract from SQL. // The Data Object with a single property public class Customer { private string m_Name = string.Empty; public string Name {

C# How to set StructLayoutAttribute.Pack via Reflection?

孤人 提交于 2020-01-14 14:14:36
问题 I am creating a C# struct dynamically via reflection, and when I examine the struct's Type in my debugger I note that the StructLayoutAttribute.Pack is defaulting to 8. I would like to set the Pack to 1. Essentially, I would like to do via reflection what can be done by adding this attribute to the declaration of a struct: [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)] I have tried using reflection after the type is created, but since the StructLayoutAttribute