reflection

Get object by reflection

倾然丶 夕夏残阳落幕 提交于 2020-01-01 12:31:19
问题 I'm looking for mechanism in c# works like that: Car car1; Car car2; Car car = (Car)SomeMechanism.Get("car1"); car1 and car2 are fields So I want to get some object with reflection, not type :/ How can I do it in c# ? 回答1: It's not possible for local variables but If you have a field, you can do class Foo{ public Car car1; public Car car2; } you can do object fooInstance = ...; Car car1 = (Car)fooInstance.GetType().GetField("car1").GetValue(fooInstance); 回答2: It looks like you're trying to

How to get a raw memory pointer to a managed class?

微笑、不失礼 提交于 2020-01-01 12:23:48
问题 How do I find a raw pointer to a managed class in C#, and, hopefully, it's raw size in memory? Obviously, this is not allowed by CLR - more precisely, strictly prohibited, as unmanaged representation of managed classes should never, ever be worked with for both stability and safe reasons - so I'm looking for a hack. I'm not looking for serializing - I do actually need a dump of managed class as it is represented in raw memory. More precisely, I'm looking for something like function

Reflection Emit: how to Convert Attribute instance to CustomAttributeBuilder or CustomAttributeData

删除回忆录丶 提交于 2020-01-01 11:58:30
问题 I made a generator class that build a proxy class based on interface which implement the interface. See my post on Build a Proxy class based on Interface without implementing it. I'm familiar with CustomAttributeData.GetCustomAttributes(MemberInfo target) , I used it when I read the Interface's members and succeed to import them to the proxy. I want to inject additional attributes to generated class in run-time. I'm asking for attributes instances to inject them into the proxy. For example: A

how to read the assembly manifest without loading the .dll

谁都会走 提交于 2020-01-01 11:55:28
问题 Essentially need to read the dependencies programmatically without loading the assembly itself, as then you can't unload them 回答1: 2 solutions come to my mind, although I think there's easier way (which I forgot or don't know :) ): 1. Load your assemblies using some additional AppDomain that you can create. Unloading whole AddDomain will also unload loaded assemblies (but only those, which were loaded using this AppDomain ). 2. Use some api, for example CCI that allows you to look inside

Using Reflection to get static method with its parameters

為{幸葍}努か 提交于 2020-01-01 11:54:50
问题 I'm using a public static class and static method with its parameters: public static class WLR3Logon { static void getLogon(int accountTypeID) {} } Now I am trying to fetch the method with its parameters into another class and using the following code: MethodInfo inf = typeof(WLR3Logon).GetMethod("getLogon", BindingFlags.Static | BindingFlags.Public | BindingFlags.FlattenHierarchy); int[] parameters = { accountTypeId }; foreach (int parameter in parameters) { inf.Invoke("getLogon", parameters

Casting to a Class which is determined at run-time

╄→尐↘猪︶ㄣ 提交于 2020-01-01 10:49:49
问题 I have a method fetchObjects(String) that is expected to return an array of Contract business objects. The className parameter tells me what kind of business objects I should return (of course this doesn't make sense in this construed case because I already said I will return Contract s, but it's basically the situation I have in my real scenario). So I get the set of entries from somewhere and load the class of the collection's entries (the type of which is specified by className ). Now I

How should I be using LambdaMetaFactory in my use case?

╄→гoц情女王★ 提交于 2020-01-01 10:18:01
问题 Despite having read all the documentation I'm aware of, I cannot resolve an issue with using lambdas to execute a method. To give a bit of background my use case is a plugin system. I'm using an annotation (@EventHandle) which can be assigned to any method. I use reflection and iterate through every method in the class and check if it has the annotation, if it does the method is added to a handler object (which is added to a list for processing every "tick"). Here is my handler class: package

How should I be using LambdaMetaFactory in my use case?

别等时光非礼了梦想. 提交于 2020-01-01 10:17:09
问题 Despite having read all the documentation I'm aware of, I cannot resolve an issue with using lambdas to execute a method. To give a bit of background my use case is a plugin system. I'm using an annotation (@EventHandle) which can be assigned to any method. I use reflection and iterate through every method in the class and check if it has the annotation, if it does the method is added to a handler object (which is added to a list for processing every "tick"). Here is my handler class: package

Custom class loading/overriding Android-native classes

放肆的年华 提交于 2020-01-01 09:05:16
问题 Main goal is to override Android system class (Activity, View etc) with my own implementation. http://android-developers.blogspot.com/2011/07/custom-class-loading-in-dalvik.html ClassLoader for custom class loading is implemented, loading non-system class (custom class) works. But when I try to load Activity with my implementation - it doesn't load, because ClassLoader already has this class in its cache: /** * Returns the class with the specified name if it has already been loaded * by the

How do you programmatically get a list of all common parameters?

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-01 09:02:17
问题 Powershell Cmdlets inherit a bunch of common parameters. Some cmdlets I write end up with predicates that depend on which parameters are actually bound. This often leads to filtering out common parameters which means you need a list of common parameter names. I also expect there to be difference in the list of common parameters from one version of powershell to another. All of this boils down to this question: How do you programmatically determine the list of common parameters? 回答1: What