CSharpCodeProvider不生成~BaseElement [英] CSharpCodeProvider does not generate ~BaseElement

查看:83
本文介绍了CSharpCodeProvider不生成~BaseElement的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这不能按预期工作:

This does not work as expected:

CodeMemberMethod finalize = new CodeMemberMethod();
finalize.Name = "Finalize";
finalize.Attributes = MemberAttributes.Override | MemberAttributes.FamilyOrAssembly;
finalize.ReturnType = new CodeTypeReference(typeof(void));
finalize.Statements.Add(new CodeMethodInvokeExpression(
     new CodeMethodReferenceExpression(
         new CodeThisReferenceExpression(), "Dispose"), new CodePrimitiveExpression(false)));



我想要的是:


What I want:

~BaseElement()
{
 Dispose(false);
}



我得到的,在C#中无效:


What I get, which isn''t valid in C#:

protected internal override void Finalize()
{
   this.Dispose(false);
}



我尝试过使用MemberAttributes.Family,但这没有用。



临时解决方法是:


I''ve tried using MemberAttributes.Family, but that doesn''t help.

A temporary workaround would be:

CodeSnippetTypeMember finalize = new CodeSnippetTypeMember("~" +
                                    Name + "() { Dispose(false); }");



其中Name显然是该类的名称,但这仅适用于C#





什么想法?


Where Name obviously is the name of the class, but that only works for C#


Ideas anyone?

推荐答案

正如我所见,代码生成器对编译器有深入的了解 - 这非常简单。如果您查看此MSDN页面: http://msdn.microsoft.com/en-us/library/ 66x5fx1b.aspx [ ^ ],你会注意到,代码生成器正在跳过一个步骤,它正在生成编译器也会生成的代码。

As I see, the code generator has deep knowledge about the compiler - and that is quite straightforward. If you look at this MSDN page: http://msdn.microsoft.com/en-us/library/66x5fx1b.aspx[^], you will notice, that the code generator is skipping a step, and it is generating the code that the compiler would also make.
Quote:

析构函数隐式调用对象基类的Finalize。因此,先前的析构函数代码被隐式转换为以下代码:

The destructor implicitly calls Finalize on the base class of the object. Therefore, the previous destructor code is implicitly translated to the following code:



...并且它得到了你得到的结果。

所以你有一个问题只有当你因某种原因需要这种语法时 - 因为语义是相同的。


...and there it comes what you get.
So you have a problem only if you want that syntax for some reason - since the semantics will be the same.


这篇关于CSharpCodeProvider不生成~BaseElement的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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