reflection

What risk does Reflection pose? (Medium Trust)

老子叫甜甜 提交于 2019-12-30 02:18:07
问题 The lack of reflection in Medium Trust hosting environments seems to cause a lot of problems for many popular web applications. Why is ReflectionPermission disabled by default with Medium Trust? What risk does reflection pose in a shared hosting environment? For random reference, see MSDN: How to use Medium Trust in ASP.NET 2.0 回答1: Reflection allows malicious code to inspect all kinds of secrets: not so much intellectual property (though sure, that too), but data that should be private and

Runtime creation of generic Func<T>

你离开我真会死。 提交于 2019-12-30 02:00:09
问题 I need to implement the method: object GetFactory(Type type); This method needs to return a Func<T> where typeparam 'T' is the 'type'. So, my problem is that I don't know how to create a Func<?> at runtime using reflection. Activator.CreateInstance doesn't work because there are no constructors on delegates. Any Ideas? 回答1: You use Delegate.CreateDelegate , i.e. from a MethodInfo ; below, I've hard-coded, but you would use some logic, or Expression , to get the actual creation method: using

what is reflection in C#, what are the benefit. How to use it to get benifit [closed]

南楼画角 提交于 2019-12-29 18:45:13
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 3 years ago . I was reading an article at msdn about reflection but i was not able to understand it even 10% about its benifit, its usage. Could you please provide some brief overview what is reflection and how can i take benefit from it. 回答1: Reflection allows you to write code that can

How is an Android activity instantiated (using reflection)?

て烟熏妆下的殇ゞ 提交于 2019-12-29 16:24:25
问题 Got asked this today in an Android interview. I answered the usual, you know, intent + startActivity, etc. Interviewer then asked more pointedly, "Yes, but where is it actually instantiated ? You don't call new Activity anywhere". Which now that I think about it, I don't really know. He hinted that it used Java reflection, but I dont have a lot of experience with that, and I've only used it to access some variables in the Android SDK. Can someone explain how Activities are instantiated using

Is it possible to get the contents of a loaded .net assembly as a byte array or stream?

半城伤御伤魂 提交于 2019-12-29 09:22:41
问题 Is it possible to get the contents of a loaded .net assembly as a byte array or stream? What I'm trying to do is something similar to (of course the real scenario is much more complex, so just storing the buffer is not an option). byte[] bytes = GetTheBytes(); Assembly asm = Assembly.Load(bytes); byte[] bytes2 = GetAssemblyAsByteArray(asm); Assert.IsTrue(bytes.SequenceEqual(bytes2)); I need to know how to implement the GetAssemblyAsByteArray function. Edit: The solution with File.ReadAllBytes

method_missing-like functionality in objective-c (i.e. dynamic delegation at run time)

≡放荡痞女 提交于 2019-12-29 09:18:09
问题 I'm trying to transform one method call into another dynamically (at runtime). For instance, I'd like the following: [obj foo] to delegate to: [obj getAttribute: @"foo"] (I'd like to do this dynamically as I don't know ahead of time what those method names or attributes are going to be). I see that there's a hook into: - (id) forwardingTargetForSelector: (SEL) aSelector That only seems to work for delegation, though, I want to keep the object as "self" and transform the method arguments.

“Object does not match target type” when calling methods using string in C#

巧了我就是萌 提交于 2019-12-29 08:47:06
问题 I'm trying to call a method using a string, but there a problem: void make_moviment(string mov,Vector3 new_mov){ GameObject past_panel = GameObject.Find(actual_level.ToString()); Type t = Type.GetType(past_panel.GetComponents<MonoBehaviour>()[0].GetType ().Name); MethodInfo method = t.GetMethod("get_answer"); method.Invoke(t,new object[] { mov })); <--- PROBLEM HERE } There's always this error "Object does not match target type" related with the last line. Do you have any recommendations? 回答1

C# Reflection get Field or Property by Name

牧云@^-^@ 提交于 2019-12-29 08:29:09
问题 Is there a way to supply a name to a function that then returns the value of either the field or property on a given object with that name? I tried to work around it with the null-coalesce operator, but apparently that doesn't like different types (which is also a bit weird to me because null is null). I could separate it it out into if nulls, but there has to be a better way to do this. Here is my function, and the two lines with Comparison objects don't compile, but I will leave them in

Can't invoke method with varargs parameters with reflection - NoSuchMethodException

天涯浪子 提交于 2019-12-29 08:13:07
问题 I'm trying to use reflection to invoke method with varargs parameters. And caught NoSuchMethodException . I couldn't figure out what is wrong here. Code: public class ReflectionTest { public ReflectionTest() { } private void varargMethod(String string, Integer ... var) { System.out.println("vargarMethod() called"); System.out.println(string + " Number of args: " + var.length + "\nContents"); for (int i = 0; i < var.length; i++) { System.out.printf(" args %d: %d", i, var[i]); //System.out

Can't invoke method with varargs parameters with reflection - NoSuchMethodException

倖福魔咒の 提交于 2019-12-29 08:13:03
问题 I'm trying to use reflection to invoke method with varargs parameters. And caught NoSuchMethodException . I couldn't figure out what is wrong here. Code: public class ReflectionTest { public ReflectionTest() { } private void varargMethod(String string, Integer ... var) { System.out.println("vargarMethod() called"); System.out.println(string + " Number of args: " + var.length + "\nContents"); for (int i = 0; i < var.length; i++) { System.out.printf(" args %d: %d", i, var[i]); //System.out