C#动态支持? [英] C# dynamic support?

查看:171
本文介绍了C#动态支持?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

和链接我还有2个问题



问题#1



什么是标准,以便我可以在c#中执行动态代码?目标类/对象必须依赖于 .net clr ?我的意思是如果有人给我一个Pascal DLL,我想我将无法使用它的代码通过动态(或将?)..所以:动态工作的要求是什么?



问题#2



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

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

有动态我可以做:

  scriptobj.Count + = 1; 

这是否意味着scriptobj的类程序员写了一个支持代码,这使得DLR = + 1

  ScriptobjClass:DynamicObject 
{
...
public override bool TryInvokeMember .when + = 1,所以做一个+ 1 ....)...
}

我错过了一些必不可少的东西。



是这样的:我们正在构建一个类,但我们也应该让.net DLR访问它 - 所以让我们写更多的代码,以便这个类还支持操作,这将允许.net用户通过dlr访问这个类?

解决方案

首先说明DLR允许您做什么,首先:



DLR有两个方面,首先是允许在运行时动态绑定对象。
例如:

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

class B
{
public int SomeMethod(int value)
{
返回值;
}
}


动态d =新A();
d.Value = 1;

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

动态d可以分配任何类型的对象,并调用其方法和属性。一个更明显的例子:

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

这适用于任何具有返回int的方法SomeMethod的类。强制类型的方法将创建一个SomeMethod接口:

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

请注意,这不是反思之前无法做到的, DLR和动态关键字使得这更简单。 DLR还为呼叫创建和缓存表达式树,因此使用DLR几乎总是比反射更有效。



然而,DLR的真正实力不仅仅是运行时间绑定,它实际上允许您创建动态行为的对象。基本上,它允许您指定当调用绑定到动态变量的实际运行时对象中不存在的方法或属性时会发生什么。这允许CLR用于动态语言,并提供与动态语言的简单互操作,创建易于使用的解析器,可以将DOM表示为动态对象,或者可以将ad-hoc查询表示为对象等。 p>

有关DLR的更多详细信息可以在其 codeplex项目文档页面。特别值得一提的是概述网站,绑定器和动态对象Interop Spec 文档。



现在您的问题:



问题1:不能用魔术与任何动态语言互动,必须有一些支持的互操作(可能有助于问题2:ScriptObjectClass使用DLR工具(通过将DynamicObject扩展为)到获取并设置Count属性,就好像它是ScriptObjectClass的实际属性(如果您好奇,请参阅网站,绑定器和动态对象互操作规范完全如何完成。)


After reading this post and links I still have 2 questions

Question #1

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 ?

Question #2

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;

Does it mean that the scriptobj's class programmers wrote a supportive code which enables DLR to do =+1 ?

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

Im missing something essential here.

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" ?

解决方案

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

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);

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();
}

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();
}

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.

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..

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.

Now to your questions:

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.)

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.)

这篇关于C#动态支持?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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