当前位置:网站首页>Revit 二次开发 HOF 方式调用transaction
Revit 二次开发 HOF 方式调用transaction
2022-07-06 08:37:00 【Imkc】
再看《C#函数式编程》一书的时候看到HOF的概念,感觉在日常开发的过程中可以使用此方式整理一个拓展方法,实现程序的快速调用。
- Transaction函数继承自
IDisposable - 我们平时开发需要使用using内部对Revit进行操作。实现自动拆卸
- 我们需要创建一个泛型函数
MTransaction并约束在IDisposable接口 - 在上述函数内部实现using关键词减少其他函数内部的反复调用,减少代码量降低耦合性
- 创建函数
TransactionHelper对此放大引用封装,通过第三方调用中间函数即可完成调用
函数结构
public static class MTransaction
{
public static void Using<TDisp>(TDisp disposable, Action<TDisp> f) where TDisp : IDisposable
{
using (disposable)
{
f(disposable);
}
}
}
public static class TransactionHelper
{
public static void Execute(Document doc, Action<IDisposable> f)
=> MTransaction.Using(new Transaction(doc, "create"), trans =>
{
trans.Start();
f(trans);
trans.Commit();
});
}
调用此方法
TransactionHelper.Execute(doc, c =>
{
var line = Line.CreateBound(new XYZ(0, 0, 0), new XYZ(2000 / 304.8, 2000 / 304.8, 0));
Wall w = Wall.Create(doc, line, new ElementId(311), false);
});
边栏推荐
- On the day of resignation, jd.com deleted the database and ran away, and the programmer was sentenced
- Sort according to a number in a string in a column of CSV file
- Image,cv2读取图片的numpy数组的转换和尺寸resize变化
- [MySQL] lock
- Day29-t77 & t1726-2022-02-13-don't answer by yourself
- MySQL learning record 07 index (simple understanding)
- Online yaml to CSV tool
- 【刷题】牛客网面试必刷TOP101
- Sublime text using ctrl+b to run another program without closing other runs
- String to leading 0
猜你喜欢

Unified ordering background interface product description Chinese garbled

2022 Inner Mongolia latest water conservancy and hydropower construction safety officer simulation examination questions and answers

MySQL learning record 10getting started with JDBC

egg. JS project deployment online server

704 二分查找
![[brush questions] top101 must be brushed in the interview of niuke.com](/img/55/5ca957e65d48e19dbac8043e89e7d9.png)
[brush questions] top101 must be brushed in the interview of niuke.com

Fairguard game reinforcement: under the upsurge of game going to sea, game security is facing new challenges

2022.02.13 - NC004. Print number of loops

Synchronized solves problems caused by sharing

目标检测——Pytorch 利用mobilenet系列(v1,v2,v3)搭建yolov4目标检测平台
随机推荐
【ROS】usb_cam相机标定
PC easy to use essential software (used)
torch建立的网络模型使用torchviz显示
JVM 快速入门
Beijing invitation media
hutool优雅解析URL链接并获取参数
[brush questions] top101 must be brushed in the interview of niuke.com
Shift Operators
Generator parameters incoming parameters
Crash problem of Chrome browser
The network model established by torch is displayed by torch viz
egg. JS directory structure
sublime text的编写程序时的Tab和空格缩进问题
Cisp-pte practice explanation
[MySQL] lock
C language double pointer -- classic question type
logback1.3. X configuration details and Practice
指针进阶---指针数组,数组指针
What is CSRF (Cross Site Request Forgery)?
游戏解包的危害及资源加密的重要性