reflection

Adding `MethodCallExpression` on top of provided `Expression`

廉价感情. 提交于 2019-12-24 09:39:45
问题 I'm having expression tree originating in Linq, e.g. leCollection.Where(...).OrderBy(...).Skip(n).Take(m) . Expression looks like: Take(Skip(OrderBy(Where(...), ...), n), m) // you got the idea Now, this is my ideal state that I have Take and Skip there, but it is not the rule. I would like to add Take / Skip programmatically if needed. I came up with way how to change Take / Skip argument, and I'm even able to add Skip under Take if I detect it's not present, but I'm struggling to figure out

How to access find all controls and all components into form in C#?

妖精的绣舞 提交于 2019-12-24 08:47:59
问题 I have a window form which contain some Controls an some Components ( like DataTable, XPCollection etc). I would like find all Control Names and Component Names which used into this form. 回答1: You could do, List<string> ctrlNames = new List<string>(); FIndAllCtrls(ctrlNames , this.Controls); private void FIndAllCtrls(ctrlNames, ControlCollection ctrlColl) { foreach(Control ctrl in ctrlColl) { ctrlNames.Add(ctrl.Name); if(ctrl.Controls.Count > 0) FIndAllCtrls(ctrlNames, ctrl.Controls); } } 回答2

How to get method parameter in method without aspect

你。 提交于 2019-12-24 08:46:47
问题 public class Test { private static void aMethod(String a, int i) { printParam(); } private static void aMethod(String a) { printParam(); } private static void printParam() { //System.out.println(gson.toJson(MethodInvocation.getArguments())); } } Is there a way, like MethodInvocation.getArguments but not use aspect(as in the print log case, the method is Changeable, so it's not that convenient), to find out the method's invocation point and print out the method parameters' value without

EF Order by Nullable DateTime using string and nested reflection

核能气质少年 提交于 2019-12-24 08:39:27
问题 According to this question I have created my methods for ordering data sets. public static IOrderedQueryable<T> Order<T>(this IQueryable<T> source, string propertyName, ListSortDirection direction = ListSortDirection.Ascending) { return ListSortDirection.Ascending == direction ? source.OrderBy(ToLambda<T>(propertyName)) : source.OrderByDescending(ToLambda<T>(propertyName)); } private static Expression<Func<T, object>> ToLambda<T>(string propertyName) { var propertyNames = propertyName.Split('

Casting items of a collection with code generation

偶尔善良 提交于 2019-12-24 08:38:48
问题 I'm doing code generation with C# and I would like to cast a backing field inside a getter. Here is an example: public class Potato { } public class ProxyPotato : Potato { } public class Stew { private ICollection<ProxyPotato> _proxyPotatoes; //This is the code I would like to generate (specialy the cast part) public ICollection<Potato> Potatoes { get { return _proxyPotatoes.Cast<Potato>().ToList(); } } } I have this code which can generate a property but I don't know how to execute a Cast:

Clear .Net Reflection cache

孤者浪人 提交于 2019-12-24 08:23:42
问题 When doing dynamic compiling of C# source code, if we happen to try to execute a compiled target DLL (for example tmp901.tmp.dll) that has a missing reference, for example: InnerException: Could not load file or assembly '_O2_Scanner_MsCatNet, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. There seems to be an issue where, for the duration of that process, that assembly will never be resolved, even if the file is copied to the location where the target

Error binding to target method in C#3.0

心已入冬 提交于 2019-12-24 07:55:45
问题 I am trying to hook Up a Delegate Using Reflection. This is what I have done so far using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml; using System.Data; using System.Threading; using System.IO; using System.Reflection; using System.Windows; namespace ChartHelper { public class ICChartHelper { public void RefreshChart() { try { Assembly myobj = Assembly.LoadFrom(@"C:\sample.dll"); foreach (Type mytype in myobj.GetTypes()) { if (mytype

Why return BAD IL FORMAT to load assembly from wcf service?

六月ゝ 毕业季﹏ 提交于 2019-12-24 07:46:48
问题 I want to load this Class library : namespace ClassLibrary1 { public class Class1 { public Class1() { } public static int Sum(int a, int b) { return a + b; } } } I have a wcf service which returns to me a byte[] array ( ClassLibrary1 ) i can not load this assembly static void Main(string[] args) { FileTransferService.ApplicationHostServiceClient client = new FileTransferService.ApplicationHostServiceClient(); FileTransferService.AssemblyPackage[] asmbs = client.GetFile(); //var newDomain =

F# How to extract keys from a boxed Map object

浪子不回头ぞ 提交于 2019-12-24 07:38:02
问题 This is a follow-up of this post and that post. I need to write a function which takes an object ( obj type) and a key (also an obj type), and if the object happens to be a Map, that is any Map<'k,'v> then extracts its keys and values. The difficulty is that I cannot parametrize the function with generic types and that we cannot pattern-match objects on generic types. I am not familiar with F# Reflection, but I found a way to get the Map's values, once I know its keys. With this example code

How to get the class Type in a base class static method in .NET?

杀马特。学长 韩版系。学妹 提交于 2019-12-24 07:37:16
问题 All I want to do is similar to: public abstract class BaseClass { public static Type GetType() { return typeof(BaseClass); } } But when I derive it public class DerivedClass : BaseClass { } I want to get Assert.AreEqual(typeof(DerivedClass), DerivedClass.GetType()); How to change the BaseClass to make this assertion true? 回答1: Static methods are not inherited. The compiler substitutes BaseClass for DerivedClass when calling a base class' static methods through one of its derived classes. For