当前位置: 首页 > 编程语言 > C#

用C#写我的第一个DSL,然后挂上func &Action分享

时间:2023-04-10 10:47:54 C#

C#学习教程:用C#编写我的第一个DSL,然后连接func和Action我正在使用构建器模式来设置复杂的父对象,但在构建父对象的子集合时遇到了一堵砖墙。这是一个例子:使用:varmyMorningCoffee=Coffee.Make.WithCream().WithOuncesToServe(16);取样(我想这就是他们所说的):varmyMorningCoffee=Coffee.Make.WithCream().PourIn(x=>{x.ShotOfExpresso.AtTemperature(100);x.ShotOfExpresso.AtTemperature(100).OfPremiumType();}).WithOuncesToServe(16);示例类(没有子PourIn()方法,因为那是我想要的Clear。)publicclassCoffee{privatebool_cream;publicCoffeeMake{getnewCoffee();}publicCoffeeWithCream(){_cream=true;归还这个;}publicCoffeeWithOuncesToServe(intounces){_ounces=ounces;归还这个;所以在我的应用程序工作中,我有构建得很好的复杂对象,但我无法在我的生活中弄清楚如何在父对象上获取子集合编码的lambda。(在这个例子中,它是Expresso的镜头(儿童系列))。也许我在这里混淆了概念,我不介意直接设置;但是,我真的很喜欢它的读法,并且想知道如何让它发挥作用。谢谢Sam如果.IncludeApps接受AppRegistrations数组怎么办?IncludeApps(paramsIAppRegistration[]apps)然后publicstaticclassApp{publicstaticIAppRegistrationIncludeAppFor(AppTypetype){returnnewAppRegistration(type);}}publicclassAppRegistration{privateAppType_type;私人布尔成本;公共AppRegistration(AppType类型){_type=类型;}publicAppRegistrationAtNoCost(){_cost=0;归还这个;.IncludeApps(App.IncludeAppFor(AppType.Any),App.IncludeAppFor(AppType.Any).AtNoCost())在IncludeApps方法中,您将检查注册并创建根据需要的对象。好的,所以我想出了如何使用额外的表达式构建器来编写我的DSL。这就是我希望我的DSL读取的方式:varmyPreferredCoffeeFromStarbucks=Coffee.Make.WithCream().PourIn(x=>{x.ShotOfExpresso().AtTemperature(100);x.ShotOfExpresso().AtTemperature(100).OfPremiumType();}).ACupSizeInOunces(16);这里是我通过的测试:x.ShotOfExpresso().AtTemperature(100).OfPremiumType();}).ACupSizeInOunces(16);Assert.IsTrue(myPreferredCoffeeFromStarbucks.expressoExpressions[0].ExpressoShots.Count==2);Assert.IsTrue(myPreferredCoffeeFromStarbucks.expressoExpressions[0].ExpressoShots.Dequeue().IsOfPremiumType==true);Assert.IsTrue(myPreferredCoffeeFromStarbucks.expressoExpressions[0].ExpressoShots.Dequeue().IsOfPremiumType==false);Assert.IsTrue(myPreferredCoffeeFromStarbucks.CupSizeInOunces.Equals(16));这是我的CoffeeExpressionBuilderDSL类:publicclassCoffee{publicListexpressoExpressions{get;私有集;}publicboolHasCream{得到;私有集;}publicintCupSizeInOunces{得到;私有集;}publicstaticCoffeeMake{get{varcoffee=newCoffee{expressoExpressions=newList()};还咖啡;}}publicCoffeeWithCream(){HasCream=true;归还这个;}publicCoffeeACupSizeInOunces(intounces){CupSizeInOunces=盎司;归还这个;}publicCoffeePourIn(Actionaction){varexpression=newExpressoExpressionBuilder();action.Invoke(表达式);expressoExpressions.Add(表达式);归还这个;}}publicclassExpressoExpressionBuilder{publicreadonlyQueueExpressoShots=newQueue();publicExpressoExpressionBuilderShotOfExpresso(){varshot=newExpressoExpression();ExpressoShots.Enqueue(镜头);归还这个;}publicExpressoExpressionBuilderAtTemperature(inttemp){varrecentlyAddedShot=ExpressoShots.Peek();最近添加的Shot.Temperature=温度;归还这个;}publicExpressoExpressionBuilderOfPremiumType(){varrecentlyAddedShot=ExpressoShots.Peek();recentlyAddedShot.IsOfPremiumType=true;归还这个;}}publicclassExpressoExpression{publicintTemperature{get;放;}publicboolIsOfPremiumType{得到;放;}publicExpressoExpression(){温度=0;IsOfPremiumType=false;}}欢迎任何和所有建议去代表路线也许这样的事情会奏效吗?varaPhone=MyPhone.Create;MyPhone.Create.IncludeApps(x=>{x.IncludeAppFor(newobject());});类MyPhone{publicMyPhoneIncludeApps(ActionincludeCommand){includeCommand.Invoke(this);返回这个;如果您没有设置代理路由,参数会起作用吗?以上就是C#学习教程:用C#编写我的第一个DSL,然后把func&Action分享的内容全部挂了。如果对大家有用,需要进一步了解C#学习教程,希望大家多多关注——varanotherPhone=MyPhone.Create.IncludeApps(newIncludeAppClass(AppType.Math),newIncludeAppClass(AppType.Entertainment)。免费());类MyPhone{内部MyPhoneIncludeApps(paramsIncludeAppClass[]includeThese){if(includeThese==null){returnthis;}foreach(includeThese中的var项目){this.Apps.Add(Item);}返回这个;}}本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:

最新推荐
猜你喜欢