DLR

在.NET Framework中,DLR2位于System.Dynamic命名空间和System.Runtime.CompilerServices命名空间的几个类中。

dynamic 类型

可以发现staticPerson出现了编译错误,而dynamicPerson并没有,因为定义为dynamic的对象可以在运行期改变其类型,甚至改变多次,这与强制转换类型是不一样的,它在编译期不会做检查。

对于dynamic 类型有两个限制:动态对象不支持扩展方法,匿名函数(lambda表达式)也不能用于动态方法调用参数,因此LINQ不能用于动态对象。大多数LINQ调用都是扩展方法,而lambda表达式用作这些扩展方法的参数。

包含DLR ScriptRuntime

项目通过NuGet安装IronPython,然后using Microsoft.Scripting.Hosting,就有了ScriptRuntime

就可以执行存储在文件中 的代码段或完整的脚本。

例:

xaml文件

<Window x:Class="DiscountWPF.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:DiscountWPF"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="300">
<Grid AutomationProperties.HelpText="disc based on cost">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions> <RadioButton x:Name="CostRadioButton" Grid.Row="0" VerticalAlignment="Center" >Disc Based on Cost</RadioButton>
<RadioButton x:Name="NumRadioButton" Grid.Row="1" VerticalAlignment="Center" >Disc Based on No of Items</RadioButton>
<StackPanel Grid.Row="2" Orientation="Horizontal" VerticalAlignment="Center">
<TextBlock>Total No of Items:</TextBlock>
<TextBox x:Name="totalItems" Width="180" HorizontalContentAlignment="Right"/>
</StackPanel>
<StackPanel Grid.Row="3" Orientation="Horizontal" VerticalAlignment="Center">
<TextBlock>Total Amount</TextBlock>
<TextBox x:Name="totalAmount" Width="178" HorizontalAlignment="Left" VerticalAlignment="Center" HorizontalContentAlignment="Right"/>
</StackPanel>
<StackPanel Grid.Row="7" Orientation="Vertical" VerticalAlignment="Center">
<Button x:Name="calDisc" Content="Calc Discount" Width="100" Click="calDisc_Click"/>
<Button x:Name="calTax" Content="Cal Tax" Width="100" Margin="0,10,0,0" Click="calTax_Click"/>
</StackPanel>
<StackPanel Grid.Row="4" Orientation="Horizontal" VerticalAlignment="Center">
<TextBlock>Discounted Amount:</TextBlock>
<TextBlock x:Name="label"></TextBlock>
</StackPanel>
<StackPanel Grid.Row="5" Orientation="Horizontal" VerticalAlignment="Center">
<TextBlock>Amount with Tax:</TextBlock>
<TextBlock x:Name="labelA"></TextBlock>
</StackPanel>
</Grid>
</Window>
using Microsoft.Scripting.Hosting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes; namespace DiscountWPF
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
} private void calDisc_Click(object sender, RoutedEventArgs e)
{
string scriptToUse;
if (CostRadioButton.IsChecked.Value)
{
scriptToUse = "AmountDisc.py";
}
else
{
scriptToUse = "CountDisc.py";
}
ScriptRuntime scriptRuntime = ScriptRuntime.CreateFromConfiguration();
ScriptEngine pythEng = scriptRuntime.GetEngine("python");
ScriptSource source = pythEng.CreateScriptSourceFromFile(scriptToUse);
ScriptScope scope = pythEng.CreateScope();
scope.SetVariable("prodCount", Convert.ToInt32(totalItems.Text));
scope.SetVariable("amt", Convert.ToDecimal(totalAmount.Text));
source.Execute(scope);
label.Text = scope.GetVariable("retAmt").ToString();
} private void calTax_Click(object sender, RoutedEventArgs e)
{
ScriptRuntime scriptRuntime = ScriptRuntime.CreateFromConfiguration();
dynamic calcRatet = scriptRuntime.UseFile("CalcTax.py");
labelA.Text = calcRatet.CalcTax(Convert.ToDecimal(label.Text)).ToString();
}
}
}

从代码可知,ScriptRuntime对象是通过配置文件来产生的,因为调用了ScriptRuntime.CreateFromConfiguration()方法产生的,因此,本项目的App.config如下:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="microsoft.scripting" type="Microsoft.Scripting.Hosting.Configuration.Section, Microsoft.Scripting"/>
</configSections>
<microsoft.scripting>
<languages>
<language names="IronPython;Python;py" extensions=".py" displayName="Python" type="IronPython.Runtime.PythonContext, IronPython"/>
</languages>
</microsoft.scripting>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
</startup>
</configuration>

Calc Discount按钮的click事件调用的方法是通过依次创建ScriptRuntimeScriptEngineScriptSource,ScriptScope对象,对ScriptSource进行执行,从ScriptScope设定变量的值,获取变量的值。

Cal Tax按钮的click事件调用的 方法是创建ScriptRuntime,然后通过该对象的UseFile方法,得到Dynamic类型对象,然后通过该动态对象,调用脚本中的方法。

CountDisc.py

discCount=5
discAmt=0.1
retAmt=amt
if(prodCount>discCount):
retAmt=amt-(amt*discAmt)

AmountDisc.py

discAmt=0.25
retAmt=amt
if amt>25:
retAmt=amt-(amt*discAmt)

CalTax.py

def CalcTax(amount):
return amount*1.075

DynamicObject和ExpandoObject

  • DynamicObject

创建自己的动态对象,要么从DynamicObject中派生,也要么使用ExpandoObject

using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace DynamicDemo
{
class Program
{
static void Main(string[] args)
{
dynamic wroxDyn = new WroxDynamicObject();
wroxDyn.FirstName = "John";
wroxDyn.LastName = "Yang";
Console.WriteLine(wroxDyn.GetType());
Console.WriteLine("{0}--{1}", wroxDyn.FirstName, wroxDyn.LastName);
Func<DateTime, string> GetTomo = today => today.AddDays(1).ToShortDateString();
wroxDyn.GetTomorrow = GetTomo;
Console.WriteLine("Tomorrow is {0}", wroxDyn.GetTomorrow(DateTime.Now));
}
}
class WroxDynamicObject : DynamicObject
{
Dictionary<string, object> _dynamicData = new Dictionary<string, object>();
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
bool success = false;
result = null;
if (_dynamicData.ContainsKey(binder.Name))
{
result = _dynamicData[binder.Name];
success = true;
}
else
{
result = "Property not found"; }
return success;
}
public override bool TrySetMember(SetMemberBinder binder, object value)
{
_dynamicData[binder.Name] = value;
return true;
}
public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
{
dynamic method = _dynamicData[binder.Name];
result = method((DateTime)args[0]);
return result != null;
}
} }

output

DynamicDemo.WroxDynamicObject
John--Yang
Tomorrow is 2022/2/5
  • ExpandoObject

ExpandoObject不用重写方法,可以直接使用,如果需要控制动态对象中属性的添加和访问,则使用DynamicObject是最佳选择,其他情况,则使用dynamic或者ExpandoObject。

using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace DynamicDemo
{
class Program
{
static void Main(string[] args)
{
dynamic wroxDyn = new ExpandoObject();
wroxDyn.FirstName = "John";
wroxDyn.LastName = "Yang";
Console.WriteLine(wroxDyn.GetType());
Console.WriteLine("{0}--{1}", wroxDyn.FirstName, wroxDyn.LastName);
Func<DateTime, string> GetTomo = today => today.AddDays(1).ToShortDateString();
wroxDyn.GetTomorrow = GetTomo;
Console.WriteLine("Tomorrow is {0}", wroxDyn.GetTomorrow(DateTime.Now));
}
}
}

output

System.Dynamic.ExpandoObject
John--Yang
Tomorrow is 2022/2/5

利用ExpandoObject,来储存任意数据类型的数据的一个Demo:

using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace DynamicDemo
{
class Program
{
static void Main(string[] args)
{
var retList = new List<dynamic>();
dynamic expObj=new ExpandoObject();
((IDictionary<string, object>)expObj).Add("john", 10);
retList.Add(expObj);
dynamic expObj1 = new ExpandoObject();
((IDictionary<string, object>)expObj1).Add("yang", DateTime.Now);
retList.Add(expObj1);
foreach(var dy in retList)
{
var tempDic = (IDictionary<string, object>)dy;
foreach(var kv in tempDic)
{
Console.WriteLine(kv.Key);
Console.WriteLine(kv.Value);
}
} }
} }

output

john
10
yang
2022/2/4 23:35:21

C#之动态语言扩展的更多相关文章

  1. Azure Table storage 之改进DynamicTableEntity类为其添加动态语言扩展

    在之前的一篇文章中提到,storage类库中包含一个可以用来动态获取Azure table storage 表结构的类-DynamicTableEntity. 我们可以通过这个类,我们无需为每一个表提 ...

  2. C#高级编程9-第12章 动态语言扩展

    C#高级编程9-第12章 动态语言扩展 dynamic t = new ExpandoObject(); t.Abc = "abc"; t.Value = ; Console.Wr ...

  3. C# 动态语言扩展(11)

    在 C# 4 开始添加 dynamic 类型.Mono C# 已经支持 C# 6.0 了. DLR C# 4 动态功能是 Dynamic Language Runtime (动态语言运行时,DLR)的 ...

  4. 【读书笔记】C#高级编程 第十二章 动态语言扩展

    (一)DLR C#4的动态功能是Dynamic Language Runtime(动态语言运行时,DLR)的一部分.DLR是添加到CLR的一系列服务. (二)dynamic类型 dynamic类型允许 ...

  5. C语言扩展动态内存报错:realloc(): invalid next size: 0x0000000002365010 ***

    晚上被这个内存扩展崩溃的问题折腾的有点崩溃,当答案揭晓的那一刻,恍然大悟,原来如此简单. 练习题目:输入一个字符串,根据字母进行排序,说白了就是一个简单的冒泡 #include <stdio.h ...

  6. Web服务器和动态语言如何交互--CGI&FastCGI&FPM浅谈

    一个用户的Request是如何经过Web服务器(Apache,Nginx,IIS,Light)与后端的动态语言(如PHP等)进行交互并将结果返回给用户的呢? 本文浅谈个人观点,可能有误,欢迎拍砖,共同 ...

  7. C# 动态语言特性,dynamic 关键字研究

    1       动态语言简介 支持动态特性的语言现在大行其道,并且有继续增长的趋势.比如 Ruby 和 Python, 还有天王级的巨星 --- JavaScript. 现在一个程序员说自己对 Jav ...

  8. javascript语言扩展:可迭代对象(3)

    除了前2篇文章中描述的可迭代对象以外,在js语言扩展中的生成器对象,也可以作为可迭代对象. 这里用到一个新的关键字yield,该关键字在函数内部使用,用法和return类似,返回函数中的一个值:yie ...

  9. JVM中的动态语言支持简介

    抽丝剥茧 细说架构那些事——[优锐课] 从版本6开始,JVM已扩展为支持现代动态语言(也称为脚本语言).Java8的发行为这一领域提供了更多动力.感到这种支持的必要性是因为Java作为一种语言固有地是 ...

  10. 动态语言运行时(DLR)

    前言 为了让C#.Visual Basic等.NET编程语言能具备动态编程语言的特性,.NET 4.0引入了一个"DLR(Dynamic Language Runtime:动态语言运行时)& ...

随机推荐

  1. AI在电子游戏中的应用:如何让虚拟世界更具沉浸感

    在过去的几十年里,电子游戏已从简单的像素化图形演变为高度复杂.视觉震撼的虚拟世界.从<超级马里奥>到<荒野大镖客2>,游戏的画面.音效和交互方式不断突破技术的极限.近年来,人工 ...

  2. autMan奥特曼机器人-代理池配置教程

    一.优势: 全可视化 稳如老牛(从2.8.6开始) 隧道代理和接口获取,使用灵活 代理池运行状态指令可查:代理池 二.启用代理池并设置服务端口 代理池的启用与关闭,均为重启autMan生效 设置隧道代 ...

  3. Flink - [04] 窗口(Windows)

    题记部分 一.Flink中的窗口是什么 (1)一般真实的流都是无界的,怎样处理无界的数据? (2)可以把无限的数据流进行切分,得到有限的数据集进行处理 -- 也就是得到有界流 (3)窗口(Window ...

  4. 删除空白行,报错 raise KeyError(list(np.compress(check, subset))) KeyError: ['姓名']

    之前这段程序跑完后没报错,经调试发现到这一句报错 > df.dropna(subset=['姓名'],inplace=True) 意思是删除'姓名'列 含有'NAN'的行 思考第一步:应该是 d ...

  5. glib-2.60在win64,msys2下编译

    前阵子,工作原因,需要在win7 64下的msys2来编译glib,下面是一些踩过的坑: 事先声明一下,这些个解决方式及纯粹是为了编译通过,可能有些做法不太适合一些需要正常使用的场合,烦请各位注意下. ...

  6. Go语言之sync包 WaitGroup的使用

    WaitGroup 是什么以及它能为我们解决什么问题? WaitGroup在go语言中,用于线程同步,单从字面意思理解,wait等待的意思,group组.团队的意思,WaitGroup就是指等待一组, ...

  7. 1、从DeepSeek API调用到Semantic Kernel集成:深度解析聊天机器人开发全链路

    引言:AI时代下的聊天机器人开发范式演进 在生成式AI技术爆发的当下,基于大语言模型(LLM)的聊天机器人开发已形成标准化技术链路.本文将结合DeepSeek API与微软Semantic Kerne ...

  8. Netty基础—8.Netty实现私有协议栈

    大纲 1.私有协议介绍 2.私有协议的通信模型 3.私有协议栈的消息定义 4.私有协议栈链路的建立 5.私有协议栈链路的关闭 6.私有协议栈的心跳机制 7.私有协议栈的重连机制 8.私有协议栈的重复登 ...

  9. 队列的实现方式(先进先出 FIFO)--环形队列

    博客地址:https://www.cnblogs.com/zylyehuo/ # -*- coding: utf-8 -*- class Queue: def __init__(self, size= ...

  10. JAVA调用Python脚本执行

    SpringBoot-web环境 <dependency> <groupId>org.springframework.boot</groupId> <arti ...