C#动态支持?动态

2023-09-05 04:13:48 作者:爱久见人心

看完this帖子和链接我仍然有2个问题的

After reading this post and links I still have 2 questions

问题1 的

什么是的标准的,这样我就可以在C#中执行的动态的code?必须在目标类/对象依赖于 .NET CLR ?我的意思是,如果有人给我一个Pascal DLL,我想我将无法通过动态使用其code(或会?)..所以:什么是动态的工作要求?

What is the criteria so that I would be able to execute dynamic code in c# ? must the target classes/objects rely on .net clr ? I mean if someone gives me a Pascal DLL , I guess I won't be able to use its code via dynamic ( or will ? )..so: What are the requirements for dynamic to work ?

问题#2 的

例如:在脚本对象 - 为了增加一个int code应该是:

For example : In script object - in order to increase an int the code should be :

 Scriptobj.SetProperty("Count", ((int)GetProperty("Count")) + 1);

通过动态的,我可以做的:

With dynamic I can do :

scriptobj.Count += 1;

这是否意味着scriptobj的类的程序员写了一个支持code 这使德国航空航天中心做 = + 1

ScriptobjClass : DynamicObject
{
  ...
  public override bool TryInvokeMember (...when +=1 so do a+1....)...
}

林失去了一些东西是必不可少的。

Im missing something essential here.

是像的我们正在构建一个类,但我们也应该让.NET DLR访问它 - 让我们多写code,使这个类的也支持操作这将允许.NET用户通过DLR 的访问该类?

Is it like : "we're building a class but we should also let .net DLR access it - so let's write more code so that this class also support operations which will allow .net users to access this class via dlr" ?

推荐答案

让我们通过解释什么DLR允许你做,先启动:

Lets start by explaining what the DLR allows you to do, first:

有两个方面的DLR第一是,它允许在运行时对象的动态结合。 例如:

There are two facets to the DLR the first is that it allows dynamic binding of objects at run time. For example:

class A
{
    public int SomeProperty { get; set; }
}

class B
{
    public int SomeMethod(int value)
    {
        return value;
    }
}


dynamic d = new A();
d.Value = 1;

d = new B();
d.SomeMethod(2);

动态D可分配任何类型的对象,并调用它们的方法和属性。更明显的例子:

The dynamic d can be assigned objects of any type and call their methods and properties. A more striking example:

int CallSomeMethod(dynamic d)
{
    return d.SomeMethod();
}

这适用于具有法的someMethod返回一个int任何类。强类型化的方式做,这将是创建一个接口的someMethod:

This works with any class that has a method "SomeMethod" returning an int. The strong typed way to do this would be create an interface with "SomeMethod":

int CallSomeMethod(IClassWithSomeMethod d)
{
    return d.SomeMethod();
}

请注意,这不是一些东西,不能与反思以前做过,但DLR和动态关键字使得这个更简单。该DLR还创建并缓存EX pression树木所以在使用DLR的叫声几乎总是比思考更有效。

Note that this is not something which could not be done before with reflection, but the DLR and the dynamic keyword makes this much simpler. The DLR also creates and caches expression trees for the calls so using the DLR is almost always more efficient than reflection.

在DLR的真正实力但不只是运行时绑定,它实际上可以让你创建一个动态行为的对象。基本上,它可以让你指定,当你调用,这并不在绑定动态变量的实际运行时对象存在的方法或属性会发生什么。这允许使用的CLR动态语言,并提供方便interoper动态LANGUES,创建易于使用的解析器可以重新present一个DOM为动态物体或ORM的,可以重新present即席查询为对象,等等。

The real strength of the DLR however is not just run time binding, it actually allows you to create objects that behave dynamically. Basically it lets you to specify what happens when you call a method or property that does not exist in the actual runtime object bound to the dynamic variable. This allows the CLR to be used for dynamic languages and to provide easy interoper with dynamic langues , create easy to use parsers which can represent a DOM as a dynamic object or ORM's that can represent ad-hoc queries as objects, etc..

有关DLR更详细的信息,可以在其的 codePLEX项目文档页面。尤其值得是概述和的网站,粘合剂,动态对象互操作规格文件

More detailed information about the DLR can be found in its codeplex project documentation page. Especially worthwhile are the overview and the Sites, Binders, and Dynamic Object Interop Spec documents.

现在您的问题:

问题1:不,你可以通过魔法与任何动态语言没有互动,必须有一些支持的互操作(可以由DLR提供便利)这样做(我不知道你所说的帕斯卡DLL的意思。 )

Question 1: No you can't interact by magic with any dynamic language, there must be some supported interop (which may be facilitated by the DLR) to do so (I don't know what you meant by Pascal dll.)

问题2:ScriptObjectClass使用DLR设施(通过扩展DynamicObject)来获取和动态设置Count属性,如果它是ScriptObjectClass的实际属性(如果你很好奇看的网站,粘合剂,动态对象互操作规格了解究竟如何做到这一点)

Question 2: ScriptObjectClass uses the DLR facilities (by extending DynamicObject) to get and set the Count property dynamically as if it was an actual property of ScriptObjectClass (if you're curious see Sites, Binders, and Dynamic Object Interop Spec for exactly how this is done.)