reflection

Using reflection to address a Linqed property

喜你入骨 提交于 2019-12-30 05:29:11
问题 I'm trying to writing a generic method that will load a record of a specific type, with a specific ID. Here's one way that works: public abstract class LinqedTable<T> where T : LinqableTable { public static T Get(long ID) { DataContext context = LinqUtils.GetDataContext<T>(); var q = from obj in context.GetTable<T>() where obj.ID == ID select obj; return q.Single<T>(); } } public abstract class LinqableTable { public abstract long ID { get; set; } } You can ignore the call to LinqUtils

How to attach event handler to an event using reflection?

自作多情 提交于 2019-12-30 04:42:09
问题 I know about EventInfo.AddEventHandler(...) method which can be used to attach handler to an event. But what should be done if i can not even define proper signature of the event handler, as in, i don't even have reference to the event args expected by the handler? I will explain the problem with the proper code. // Scenario when I have everything available in my solution, Zero Reflection Scenario. internal class SendCommentsManager { public void Customize(IRFQWindowManager rfqWindowManager)

Access unexported fields in golang/reflect?

旧巷老猫 提交于 2019-12-30 04:29:08
问题 Is there a way to use Reflect to access unexported fields in go 1.8? This no longer seems to work: https://stackoverflow.com/a/17982725/555493 Note that reflect.DeepEqual works just fine (that is, it can access unexported fields) but I can't make heads or tails of that function. Here's a go playarea that shows it in action: https://play.golang.org/p/vyEvay6eVG. The src code is below import ( "fmt" "reflect" ) type Foo struct { private string } func main() { x := Foo{"hello"} y := Foo{"goodbye

Command completion in mathematica : suggest rules/options

安稳与你 提交于 2019-12-30 04:20:08
问题 In current version of Mathematica these keyboard shortcuts are quite handy Ctrl+K completes current command GraphPl -> press Ctrl+K -> GraphPlot Ctrl+Shift+K completes current command and adds argument placeholders which could be replaced with actual values with tab key GraphPl -> press Ctrl+Shift+K -> GraphPlot[{vi1->vj1,vi2->vj2,...}] However I couldn't find any keyboard option to show associated settings/options For instance Say If I need to plot a graph with different layouts, I know I

How do I get the executing object for a stackframe?

淺唱寂寞╮ 提交于 2019-12-30 04:18:25
问题 When using reflection it is possible to obtain the call stack (apart from that it can be a crude approximation due to JIT optimizations) using System.Diagnostics.StackTrace and examine the StackFrame objects contained. How can I get a reference to the object (the this-pointer) on which a method in a stack frame is executing? I know I can get the MethodBase by calling GetMethod() on the stack frame object, but what I'm looking for is something along the lines of GetObject() (which'd naturally

Checking if Type or instance implements IEnumerable regardless of Type T

拥有回忆 提交于 2019-12-30 03:45:09
问题 I'm doing a heavy bit of reflection in my current project, and I'm trying to provide a few helper methods just to keep everything tidy. I'd like to provide a pair of methods to determine if a type or instance implements IEnumerable – regardless of the type T . Here is what I have at the moment: public static bool IsEnumerable(this Type type) { return (type is IEnumerable); } public static bool IsEnumerable(this object obj) { return (obj as IEnumerable != null); } When I test them using Debug

How do you find only properties that have both a getter and setter?

瘦欲@ 提交于 2019-12-30 03:45:05
问题 C#, .NET 3.5 I am trying to get all of the properties of an object that have BOTH a getter and a setter for the instance. The code I thought should work is PropertyInfo[] infos = source.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.SetProperty | BindingFlags.GetProperty); However, the results include a property that does not have a setter. To give you a simple idea of my inheritance structure that might be affecting this (though I don't know how): public

Reflective Web Application (WebIDE)

不羁岁月 提交于 2019-12-30 03:36:03
问题 Preamble So, this question has already been answered, but as it was my first question for this project, I'm going to continue to reference it in other questions I ask for this project. For anyone who came from another question, here is the basic idea: Create a web app that can make it much easier to create other web applications or websites. To do this, you would basically create a modular site with "widgets" and then combine them into the final display pages. Each widget would likely have

How can I build a 'dependency tree diagram' from my .NET solution

自古美人都是妖i 提交于 2019-12-30 03:13:43
问题 I can get easily see what projects and dlls a single project references from within a Visual Studio .NET project. Is there any application or use of reflection that can build me a full dependency tree that I can use to plot a graphical chart of dependencies? 回答1: In addition to NDepend, you can also try this addin for Reflector for showing assembly dependency graph. 回答2: NDepend comes with an interactive dependency graph coupled with a dependency matrix. You can download and use the free

Convert Expression<Func<T1,bool>> to Expression<Func<T2,bool> dynamically

假如想象 提交于 2019-12-30 02:20:07
问题 I cannot find the way to convert from Expression<Func<T1,bool>> to Expression<Func<T2,bool>>. Since Im using a lot of reflection, in fact, what I really need is a method which takes a type parameter and performs the conversion. public object Convert(Expression<Func<T1,bool>> expr, Type t); T2 is derived from T1 public class T1 { int FamilyId {get; set;} } public class T2 : T1 { ... other properties } I am defining a filter expression on the base class Expression<Func<T1,bool>> filter = p => p