知乎周源微信

A few weeks ago I interviewed Steven Frank (blog), co-owner of Panic and a Mac Developer (who I went to college with). After that interview I stumbled upon the very recently release NSDuctTape project. First, how can you not like a project named after Duct Tape. Second, whenever I hear that some code will bridge to completely incongruent and unbridgeable things, I gotta check it out. What can I say, if there's a freak somewhere that promises to tape two things together, I want to see it! ;) (And I mean freak in the most positive way!)

几周前,我采访了Panic的共同所有者,Mac开发人员(与我一起上大学)的Steven Frank (博客)。 采访之后,我偶然发现了最近发布的NSDuctTape项目。 首先,您怎么可能不喜欢以Duct Tape命名的项目。 其次,每当我听到一些代码将桥接到完全不一致和不可桥接的内容时,我就必须进行检查。 我要说的是,如果某个地方有一个怪胎,承诺将两件事绑在一起,我想看看! ;)(我的意思是最积极的态度!)

NSDuctTape is niche, to be clear, but if you want to write .NET code using Mono on the Mac and you want access to the Objective C Cocoa libraries, this is your one-stop shop. (NOTE: If you do download his source, you'll likely have to pull the files out one at a time because there's Mac files in the zip with the same names as folders and Windows doesn't like it.)

NSDuctTape很明显,但是如果您想在Mac上使用Mono编写.NET代码,并且想要访问Objective C Cocoa库,这是您的一站式商店。 (注意:如果您确实下载了他的源代码,则可能必须一次将一个文件拉出,因为zip中有Mac文件,其名称与文件夹相同,而Windows不喜欢。)

And so, Dear Reader, I present to you sixteenth in a infinite number of posts of "The Weekly Source Code." Here's some source I was reading this week.

因此,尊敬的读者,我每周源代码的无数帖子中向您展示了第十六位。 这是我本周正在阅读的一些资料。

Dave, the author, hasn't check on Mono's support for Linq, but he uses C# 3.0 features to create his own LINQ-lite helper methods. I found this to be a clever "punt."

作者Dave尚未检查Mono对Linq的支持,但他使用C#3.0功能来创建自己的LINQ-lite辅助方法。 我发现这是一个聪明的“平底锅”。


internal static class Enumerable
{public static IEnumerableSelect(IEnumerablelist, Converterconvert){foreach (TInput value in list)yield return convert(value);}public static IEnumerableSelectMany(IEnumerablelist, Converter> convert) { foreach (TInput value in list) foreach (TOutput converted in convert(value)) yield return converted; } public static IEnumerableWhere(IEnumerablelist, Predicatepredicate) { foreach (T value in list) if (predicate(value)) yield return value; } public static ListToList(IEnumerablelist) { Listresult = list as List; return result ?? new List(list); } public static T[] ToArray(IEnumerablelist) { return ToList(list).ToArray(); } }

Because he's "thunking" (not the technically accurate word, but I like saying it) down into unmanaged code that needs to have handles allocated and deallocated, he creates an HGlobal wrapper class using my most favorite .NET BCL pattern, IDisposable. Classic stuff, simple and works great.

因为他在“ thunking”(不是技术上准确的词,但我喜欢说),变成需要分配和释放句柄的非托管代码,所以他使用我最喜欢的.NET BCL模式IDisposable创建了HGlobal包装器类。 经典的东西,简单,效果很好。

...snip...public void Dispose()
{if (_hGlobal != IntPtr.Zero)Marshal.FreeHGlobal(_hGlobal);_hGlobal = IntPtr.Zero;
}private DisposableHGlobal(IntPtr hGlobal)
{_hGlobal = hGlobal;
}public static DisposableHGlobal StructureToHGlobal(T value)where T : struct
{DisposableHGlobal result = new DisposableHGlobal(Marshal.SizeOf(value));Marshal.StructureToPtr(value, result.ToIntPtr(), false);return result;
}
...snip...

Finally, in his application managed "wrapper" he spins through his chosen System.Types and registers each of them with ObjectiveC. This interop is one way, meaning that he's choosing to expose his .NET types as Objective C classes.

最后,在他的应用程序管理的“包装器”中,他浏览了自己选择的System.Types并向ObjectiveC注册了它们。 这种互操作是一种方式,这意味着他选择将.NET类型公开为Objective C类。

public static void Run(string nibFile, IEnumerableexposedTypes)
{ObjectiveCClass nsAutoReleasePoolClass = ObjectiveCClass.GetClass("NSAutoreleasePool");IntPtr autoReleasePool = nsAutoReleasePoolClass.Instantiate();ObjectiveCMethods.SendMessage(autoReleasePool, ObjectiveCMethods.SelectorFromString("init"));try{IntPtr process = IntPtr.Zero;GetCurrentProcess(ref process);TransformProcessType(ref process, ProcessType.ForegroundApplication);SetFrontProcess(ref process);Registrar.Initialize();foreach (Type type in exposedTypes){ObjectiveCNameAttribute attribute = MemberInfoUtility.GetCustomAttribute(type);Registrar.RegisterClass(attribute != null ? attribute.Name : type.Name, type);}ObjectiveCClass nsBundleClass = ObjectiveCClass.GetClass("NSBundle");IntPtr name = NativeString.StringToNativeString(nibFile);ObjectiveCClass nsDictionaryClass = ObjectiveCClass.GetClass("NSDictionary");IntPtr key = NativeString.StringToNativeString("NSOwner");ObjectiveCClass nsApplicationClass = ObjectiveCClass.GetClass("NSApplication");IntPtr sharedApplication = ObjectiveCMethods.SendMessage(nsApplicationClass.ToIntPtr(), ObjectiveCMethods.SelectorFromString("sharedApplication"));IntPtr nsDictionary = ObjectiveCMethods.SendMessage(nsDictionaryClass.ToIntPtr(), ObjectiveCMethods.SelectorFromString("dictionaryWithObject:forKey:"), sharedApplication, key);IntPtr zone = ObjectiveCMethods.SendMessage(sharedApplication, ObjectiveCMethods.SelectorFromString("zone"));ObjectiveCMethods.SendMessage(nsBundleClass.ToIntPtr(), ObjectiveCMethods.SelectorFromString("loadNibFile:externalNameTable:withZone:"), name, nsDictionary, zone);ObjectiveCMethods.SendMessage(sharedApplication, ObjectiveCMethods.SelectorFromString("run"));}finally{ObjectiveCMethods.SendMessage(autoReleasePool, ObjectiveCMethods.SelectorFromString("release"));autoReleasePool = IntPtr.Zero;}
}

It's inside the RegisterClass where he creates Objective C classes for each .NET class, lazily making class definitions, and poking values into them. He's using "reflection" on both sides...reflecting over the .NET types, methods, etc and dynamically creating the same types, methods, etc on the ObjectiveC side.

在RegisterClass内部,他为每个.NET类创建Objective C类,懒惰地创建类定义,然后将值放入其中。 他在两边都使用“反射” ...反射.NET类型,方法等,并在ObjectiveC方面动态创建相同的类型,方法等。

Freaky and fun!

怪异而有趣!

翻译自: https://www.hanselman.com/blog/the-weekly-source-code-16-duct-tape-edition

知乎周源微信

知乎周源微信_每周源代码16-风管磁带版相关推荐

  1. 知乎周源微信_每周源代码7

    知乎周源微信 In my new ongoing quest to read source code to be a better developer, I now present the seven ...

  2. 知乎周源微信_每周源代码36-PDC,BabySmash和Silverlight图表

    知乎周源微信 First, let me remind you that in my new ongoing quest to read source code to be a better deve ...

  3. 知乎周源微信_每周源代码33-Google Chrome中的Microsoft Open Source

    知乎周源微信 First, let me remind you that in my new ongoing quest to read source code to be a better deve ...

  4. 知乎周源微信_每周源代码42-树修剪,插件和MEF

    知乎周源微信 I really advocate folks reading as much source as they can because you become a better writer ...

  5. 知乎周源微信_每周源代码56-Visual Studio 2010和.NET Framework 4培训套件-代码合同,并行框架和COM互操作...

    知乎周源微信 Do you like a big pile of source code? Well, there is an imperial buttload of source in the V ...

  6. 知乎周源微信_每周源代码39-Silverlight 3中的Commodore 64仿真器

    知乎周源微信 I had the pleasure of interviewing Pete Brown this last week and talking about the Silverligh ...

  7. 知乎周源微信_每周源代码24-可扩展性版本-.NET中的插件,提供程序,属性,插件和模块...

    知乎周源微信 I've been getting more and more interested in how folks extend their applications using plugi ...

  8. 知乎周源微信_每周源代码18-深度缩放(Seadragon)Silverlight 2 MultiScaleImage鼠标滚轮缩放和平移版...

    知乎周源微信 Dear Reader, I present to you eighteenth in a infinite number of posts of "The Weekly So ...

  9. 知乎周源微信_每周源代码30-具有XML文字的VB.NET作为ASP.NET MVC的视图引擎

    知乎周源微信 I was literally in the middle of writing the post when I saw a message from Andrew Davey abou ...

  10. 知乎周源微信_每周源代码59-开源宝藏:具有讽刺意味的.NET语言实现工具包

    知乎周源微信 One of the best, if not the best way to sharpen the saw and keep your software development sk ...

最新文章

  1. python 常见函数_Python基础函数:初学者常用的十个Python函数,非常全面!
  2. 【ASP】Menu菜单导航
  3. Servlet生命周期中的service方法分析
  4. Android 系统(211)---Power键不亮屏分析方法
  5. 在安卓手机上编写和运行Python 3.x程序
  6. Hive 与 RDBMS的区别
  7. 单主复制与多主复制入门
  8. Servlet面试题18道
  9. LANDrop局域网文件传输神器
  10. matlab中饱和函数怎么写,matlab中饱和函数如何表示呢
  11. 360无线网卡驱动linux,Kali Linux安装360免费wifi驱动。
  12. 王老吉为何败给加多宝?
  13. 澳洲语言成绩等级c,澳洲本科成绩等级介绍 怎么划分的
  14. 第二章——Swift语言
  15. arcgis通俗易懂教程(一)------入门教程
  16. R语言:无法精确计算带连结的p值
  17. 中国互联网+化妆品行业深度调研及投资机会分析报告
  18. 唐僧是怎么管理孙悟空的?
  19. python获取二进制文件流,压缩并下载
  20. 蓄电池内阻测试仪分析软件,蓄电池内阻测试仪的产品特点与参数

热门文章

  1. iQOO手机如何打开高清通话volte?
  2. 历史为什么选择C语言?事实证明:暮年的C语言,依旧宝刀未老!
  3. 泰拉瑞亚服务器搭建-瑟银灾厄-Centos
  4. 对于导入UE4中的模型坐标原点不在物体中心的解决办法
  5. Android 系统(93)---android 怎么判断手机号是移动还是联通还是电信
  6. Adyen海外支付 - Adyen回调
  7. 去除水晶报表小数点最后多余的0
  8. 云计算与分布式技术-常见云的比较
  9. 2022-2028年全球与中国工业用智能眼镜行业产销需求与投资预测分析
  10. 论文笔记:SRF(stereo radiance fileds)