codedom

CodeDom generic type constraint

只谈情不闲聊 提交于 2019-12-09 14:51:40
问题 Is there a way to generate a class constraint with CodeDom. Because when I use something like var method = new CodeMemberMethod(); var genericParam = new CodeTypeParameter("InterfaceType"); genericParam.Constraints.Add("class"); method.TypeParameters.Add(genericParam); the generated code is like private InterfaceType GetImpl<InterfaceType>() where InterfaceType : @class { } The best workaround i found is to use a leading whitespace before the class genericParam.Constraints.Add(" class"); But

Create an .EXE from an .EXE

若如初见. 提交于 2019-12-08 18:54:31
So what I want to do is create an executable in a program in vb. What I mean by this is that there is a program that can output a .EXE. What I try to do is to create a program that can output a smaller program that runs predefined commands. All I know is that it is somehow possible with a STUB. That you open the STUB and add extra code to it and save it again as an .EXE. You can use compiler services to create EXE at run time. CSharpCodeProvider : Provides access to instances of the C# code generator and code compiler. Dynamic Creation of Assemblies/Apps System.CodeDom.Compiler 来源: https:/

Parsing C# source code

穿精又带淫゛_ 提交于 2019-12-08 17:07:27
问题 I'm trying to parse a simple.cs source file using the following code: CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp"); var compileUnit = provider.Parse(File.OpenText(filename)); This gives me a NotImplementedException: "This CodeDomProvider does not support this method" Is it true that .NET does not provide an implemenation for parsing C# code? Or am I just using this class the wrong way? Edit: The reason for doing this is that I want to toy around with some methods for

C# CodeDom Automatic Property

烈酒焚心 提交于 2019-12-08 15:41:05
问题 I have a property created with CodeDom. How can I set it to being an automatic property instead of adding CodeFieldReferenceExpressions against a private member? 回答1: IIRC, CodeDom simply doesn't have a way of expressing this. Automatically implemented properties are just compiler sugar, but since it doesn't map (cleanly) to all languages, it doesn't fit cleanly into CodeDom (besides, CodeDom would have needed an update). 回答2: Yes you can. You can use CodeSnippetTypeMember class for that

Dynamically execute string as code in C#

有些话、适合烂在心里 提交于 2019-12-08 12:08:34
问题 I need to convert string to executable code. The string is in foreach statement. foreach (InsuredItem _i in p.InsuredItems) { string formula = "(_i.PremiumRate/100)*SumAssured"; _i.Premium = (Execute formula); } The formula is loaded from setup. this is just a demonstration. I need to execute the string in foreach loop. Thanks. 回答1: Assuming that your formula is valid C# code and that it uses a known set of local variables (so that you can create a "globals" type containing all of them), you

Create an .EXE from an .EXE

落花浮王杯 提交于 2019-12-08 06:24:32
问题 So what I want to do is create an executable in a program in vb. What I mean by this is that there is a program that can output a .EXE. What I try to do is to create a program that can output a smaller program that runs predefined commands. All I know is that it is somehow possible with a STUB. That you open the STUB and add extra code to it and save it again as an .EXE. 回答1: You can use compiler services to create EXE at run time. CSharpCodeProvider : Provides access to instances of the C#

Unable to use Visual Studio project on different computer

一世执手 提交于 2019-12-08 05:38:39
问题 I was working on a project (winforms, C++/Cli) on my desktop. When I copy project files to use it in my laptop, designer do not open. References to the 3rd party library[MetroFramework] is properly linked (project compile & run fine, no build error) but if I open designer I receive the following error C++ CodeDom parser error: Line:99,Column:27 --unknown type".please make sure that the assembly that contains this type is referenced. if this is a part of your development project, make sure

how to create an interface method with CodeDom

送分小仙女□ 提交于 2019-12-08 03:34:47
问题 I am using CodeDom to generate my WCF service interfaces and implementation files. I cannot find any options on CodeMemberMethod that will allow me to generate an interface method. If I use MemberAttributes.Abstract I am almost there but it includes the abstract keyword which is invalid for an interface. Does anyone know the correct syntax to generate the method? Here is the code I have developed to generate the interface file: private void CreateServiceContractFile(Type entity, BoElement bo,

Finalizer with CodeDom?

…衆ロ難τιáo~ 提交于 2019-12-08 00:48:10
问题 Is it possible to add a Finalizer to a CodeDom generated class (other than using CodeSnippetTypeMember)? I couldn't find any information about it on MSDN. 回答1: This was a known bug in .NET Framework and was reported some time back at http://lab.msdn.microsoft.com/ProductFeedback/viewFeedback.aspx?feedbackId=FDBK48431 But the link above is currently not working. You can view it using internet archive on the following link http://web.archive.org/web/20080224200911rn_2/connect.microsoft.com

Should Type.GetType(string) be aware of dynamically generated types?

对着背影说爱祢 提交于 2019-12-07 19:43:20
问题 I have an app which creates some code using CodeDom compiler. I can see that the generated assembly is in memory. But when I call Type.GetType(typeName), it returns null. I find this a little bit confusing. What am I doing wrong? static void Main(string[] args) { // FYI: Code is some dummy class with only 1 instance method. string code = System.IO.File.ReadAllText("CodeToCompile.cs.txt"); string errors = null; Assembly asm = DynamicCompiler.Compile(code, generateInMemory: true,