C#反思 - 最近有人使用它吗? [英] C# Reflection--anybody use it lately?

查看:140
本文介绍了C#反思 - 最近有人使用它吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C#反射有什么用,除了找出哪些类型在

一个程序集?并动态调用程序集中的方法(.dll

或.exe)?


此外,奖金问题,您可以使用Reflection构建编译器吗?一个

将动态构建用户定义的类。 (字面意思是,

用户定义一个类,实例化它,并从控制台运行它

模式,一直由程序提示)?


我想是这样,但我的最后一个问题是,是否有人使用过

反思。似乎有些人动态地使用反射来调用程序集(.dll或.exe)中的方法,这对于使用旧的非C#非托管代码可能很有用。




RL


Reflection API允许C#程序检查和操作

本身。它可用于有效地查找

程序集中的所有类型和/或动态调用程序集中的方法。它可以在
次甚至用于动态发出中间语言代码所以

生成的代码可以直接执行。

What good is C# Reflection, other than to find out what types are in
an assembly? And to dynamically invoke methods in an assembly (.dll
or .exe)?

Also, bonus question, can you use Reflection to build a compiler? One
that will construct a user defined class "on the fly" (literally, the
user defines a class, instantiates it, and runs it from the console
mode, all the while prompted by the program)?

I guess so, but my final question is whether anybody has used
Reflection. Seems that some people use reflection to dynamically
invoke methods in an assembly (.dll or .exe), which might be useful
for using old non-C# unmanaged code.

RL

The Reflection API allows a C# program to inspect and manipulate
itself. It can be used to effectively find all the types in an
assembly and/or dynamically invoke methods in an assembly. It can at
times even be used to emit Intermediate Language code on the fly so
that the generated code can be executed directly.

推荐答案

Google for PostSharp,然后你会看到反射不是关于创建实例和调用方法的
。另外我使用了一个对象

持久性框架,它描述了使用.NET属性的UML模型

www.capableobjects.com )所以我觉得反射也非常有用。


至于创建类,你想使用AssemblyBuilder等。


Pete

Google for PostSharp, then you will see that reflection isn''t all about
creating instances and invoking methods. Additionally I used an object
persistence framework which describes the UML model using .NET attributes
(www.capableobjects.com) so I find reflection very useful there too.

As for creating classes you want to use AssemblyBuilder etc.

Pete


7月16日凌晨1点49分,Peter Morris < mrpmorri ... @ SPAMgmail.comwrote:
On Jul 16, 1:49 am, "Peter Morris" <mrpmorri...@SPAMgmail.comwrote:

Google for PostSharp,那么你会看到反映不是全部关于

创建实例和调用方法。另外我使用了一个对象

持久性框架,它描述了使用.NET属性的UML模型

www.capableobjects.com )所以我觉得反射也非常有用。


至于创建类,你想使用AssemblyBuilder等。


Pete
Google for PostSharp, then you will see that reflection isn''t all about
creating instances and invoking methods. Additionally I used an object
persistence framework which describes the UML model using .NET attributes
(www.capableobjects.com) so I find reflection very useful there too.

As for creating classes you want to use AssemblyBuilder etc.

Pete



谢谢Pete Morris。我在
上查看了AOP的页面 http:// en。 wikipedia.org/wiki/Aspect-...ed_programming ,好像

喜欢gobblygook。一个具体的例子会有所帮助,但似乎关注的是安全问题。例如,如果其中写入的程序是用AOP编写的,那么那些恶意攻击系统的人应该停止做其他不好的b / b $ b的东西。
反思很多,动态检查,类似于数据包嗅探,所有

翻译单位,类,代码块等在真正的时间运行
时间。然后黑客可以被停止,因为他可能没有

权限(即线程权限)在

成功登录网络后做任何不好的事情。


简而言之,无所事事。我会把它从我的物品清单中删除

来学习。


RL

Thanks "Pete Morris". I checked out the page on AOP in
http://en.wikipedia.org/wiki/Aspect-...ed_programming, and it seems
like gobblygook. A specific example would have helped, but it seems
that the concern is over security. For example, somebody who
maliciously hacks into a system should be stopped from doing other bad
stuff if the programs written therein are written in AOP, which uses
reflection a lot to dynamically examine, akin to packet sniffing, all
translation units, classes, blocks of code etc being run in real
time. The hacker can then be stopped because he might not have the
right permission (i.e., thread permission) to do anything bad after
successfully logging into a network.

In short, much ado about nothing. I''ll cross it off my list of things
to study.

RL

7月16日,12:34 * pm,raylopez99< raylope ... @ yahoo.comwrote:
On Jul 16, 12:34*pm, raylopez99 <raylope...@yahoo.comwrote:

C#反射有什么好处,除了找出

组装中的类型? *并动态调用程序集中的方法(.dll

或.exe)?
What good is C# Reflection, other than to find out what types are in
an assembly? *And to dynamically invoke methods in an assembly (.dll
or .exe)?



你不能动态调用程序集中的方法,你动态地调用类的方法(或者更常见的是一个对象)。


是的,这很有用。标准库中使用

的一些东西是数据绑定(所有这些 - WinForms,ASP.NET,WPF)和

序列化(二进制和XML) 。第三方控制反转

容器(如Unity)通常也会使用它


在我自己的一面,我曾编写过显示树的代码通过反射在TreeView中对象

(所有不同类型) - 它读取自定义

属性关闭属性以确定应该使用哪些属性

显示在树中以及如何以及可以使用哪些属性

来检索对象的可见子项。

You don''t dynamically invoke methods in an assembly, you dynamically
invoke methods of a class (or, more often, of an object).

And yes, this is useful. A few things in the standard library that use
it are data binding (all of it - WinForms, ASP.NET, WPF) and
serialization (both binary and XML). Third-party inversion of control
containers (such as Unity) also typically use it

On my own side, I once wrote code that displayed a tree of objects
(all of different types) in a TreeView via reflection - it read custom
attributes off properties to figure out which properties were supposed
to be displayed in a tree and how, and which properties could be used
to retrieve visible children of object.


此外,奖金问题,你可以使用Reflection来构建编译器吗? *一个

将构建一个用户定义的类在运行中。 (从字面上看,

用户定义一个类,实例化它,并从控制台运行它

模式,一直由程序提示)?
Also, bonus question, can you use Reflection to build a compiler? *One
that will construct a user defined class "on the fly" (literally, the
user defines a class, instantiates it, and runs it from the console
mode, all the while prompted by the program)?



是的。请参阅System.Reflection.Emit。

Yes. See System.Reflection.Emit.


我想是的,但我的最后一个问题是是否有人使用了

反射。似乎有些人动态地使用反射来调用程序集(.dll或.exe)中的方法,这对于使用旧的非C#非托管代码可能很有用。


I guess so, but my final question is whether anybody has used
Reflection. Seems that some people use reflection to dynamically
invoke methods in an assembly (.dll or .exe), which might be useful
for using old non-C# unmanaged code.



您不能使用反射来调用方法 (你的意思是,函数)

一个非托管的DLL。

You cannot use reflection to invoke "methods" (you mean, functions) in
an unmanaged DLL.


这篇关于C#反思 - 最近有人使用它吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆