top of page

undiscovered Group

Public·30 members

Metaprogramming In .NET

Metaprogramming in .NET is designed to help readers understand the basic concepts, advantages and potential pitfalls of metaprogramming. It introduces core concepts in clear, easy-to-follow language and then it takes you on a deep dive into the tools and techniques you'll use to implement them in your .NET code. You'll explore plenty of real-world examples that reinforce key concepts. When you finish, you'll be able to build high-performance, metaprogramming-enabled software with confidence.

Metaprogramming in .NET

Download Zip:

When you write programs that create or modify other programs, you are metaprogramming. In .NET, you can use reflection as well as newer concepts like code generation and scriptable software. The emerging Roslyn project exposes the .NET compiler as an interactive API, allowing compile-time code analysis and just-in-time refactoring.

Metaprogramming in .NET is a practical introduction to the use of metaprogramming to improve the performance and maintainability of your code. This book avoids abstract theory and instead teaches you solid practices you'll find useful immediately. It introduces core concepts like code generation and application composition in clear, easy-to-follow language, and then it takes you on a deep dive into the tools and techniques that will help you implement them in your .NET applications.

Metaprogramming API provided by .NEXT library allows to generate and execute code in runtime. Code generation object model is language agnostic so developer can use it from any .NET programming language. From design point of view, metaprogramming capabilities built on top of LINQ Expressions without direct usage of IL generator. This increases portability of the library between different .NET implementations. All custom expressions introduced by Metaprogramming libary are reducible into predefined set of LINQ Expressions.

In the commonality/variability analysis, automatic metaprogramming occupies an interesting place. In the Figure 2 example, it places structure and behavior (the outline of the class above) into commonality, allowing for variability along data/type lines, that of the type being stored in the generated class. Clearly, we can swap in any type desired into the ListOf type.

Metaprogramming is possible in .NET (see compiler compilers, regular expressions, code DOM, reflection etc.) but C# is not capable of template metaprogramming because it does not have that language feature.

No, metaprogramming of this complexity is not supported directly by the C# language. However, like @littlegeek said, the Text Template Transformation Toolkit included with Visual Studio will allow you to achieve code generation of any complexity.

A way around this is to do metaprogramming from outside the language, using program transformation tools. Such tools can parse source code, and carry out arbitrary transformations on it (that's what metaprogramming does anyway) and then spit out the revised program.

If you have a general purpose program transformation system, that can parse arbitrary languages, you can then do metaprogramming on/with whatever language you like.See our DMS Software Reengineering Toolkit for such a tool, that have robust front ends for C, C++, Java, C#, COBOL, PHP and a number of other programming langauges, and has been used for metaprogramming on all of these.

Yesterday I came across Manifold, an extension of the Java language that, among other things (some of which can already be done with C#), allows for what I later discovered to be called metaprogramming.

I'm still very interested in seeing metaprogramming or something of similar nature in C# and/or the the other .NET languages. Perhaps I should wait until Microsoft intends to implement this stuff in .NET?

Metaprogramming can be used to move computations from run-time to compile-time, to generate code using compile time computations, and to enable self-modifying code. The ability of a programming language to be its own metalanguage is called reflection.[4] Reflection is a valuable language feature to facilitate metaprogramming.

Metaprogramming enables developers to write programs and develop code that falls under the generic programming paradigm. Having the programming language itself as a first-class data type (as in Lisp, Prolog, SNOBOL, or Rebol) is also very useful; this is known as homoiconicity. Generic programming invokes a metaprogramming facility within a language by allowing one to write code without the concern of specifying data types since they can be supplied as parameters when used.

Not all metaprogramming involves generative programming. If programs are modifiable at runtime or if incremental compilation is available (such as in C#, Forth, Frink, Groovy, JavaScript, Lisp, Elixir, Lua, Nim, Perl, PHP, Python, REBOL, Ruby, Rust, SAS, Smalltalk, and Tcl), then techniques can be used to perform metaprogramming without actually generating source code.

One style of generative approach is to employ domain-specific languages (DSLs). A fairly common example of using DSLs involves generative metaprogramming: lex and yacc, two tools used to generate lexical analyzers and parsers, let the user describe the language using regular expressions and context-free grammars, and embed the complex algorithms required to efficiently parse the language.

Some argue that there is a sharp learning curve to make complete use of metaprogramming features.[8] Since metaprogramming gives more flexibility and configurability at runtime, misuse or incorrect use of the metaprogramming can result in unwarranted and unexpected errors that can be extremely difficult to debug to an average developer. It can introduce risks in the system and make it more vulnerable if not used with care. Some of the common problems which can occur due to wrong use of metaprogramming are inability of the compiler to identify missing configuration parameters, invalid or incorrect data can result in unknown exception or different results.[9] Due to this, some believe[8] that only high-skilled developers should work on developing features which exercise metaprogramming in a language or platform and average developers must learn how to use these features as part of convention.

Loosely inspired by F# type providers, C# source generators respond to the same aim of enabling metaprogramming but in a completely different way. Indeed, while F# type providers emit types, properties, and methods in-memory, source generators emit C# code back into the compilation process.

In the presentation I tried to introduce some basic concepts of functional programming (immutable values, lazy evaluation) to the audience with no experience with functional programming, as well as present some of the most interesting features of F# (like strict type system based on type inference, .NET interoperability and metaprogramming). The whole contents of the presentation is following:

Nemerle is a general-purpose, multi-paradigm programming language for the .Net platform. It is as easy to learn and use as C# or VB.NET but Nemerle is by far more powerful. One may start using it as an advanced C# and then, as learning goes on, employ a range of cool features enabling metaprogramming and functional programming. The metaprogramming is based on macros bearing some similarity to Lisp.

With Roslyn, you can easily create dynamic code that does complex activities. Rather than resorting to System.Reflection.Emit and IL, you can simply write your C# and compile that instead. The barrier to entry to perform powerful metaprogramming-based implementations is much lower with Roslyn.

DiffSharp is an algorithmic differentiation or automatic differentiation (AD)library for the .NET ecosystem, which is targeted by the C# and F# languages,among others. The library has been designed with machine learning applicationsin mind, allowing very succinct implementations of models and optimizationroutines. DiffSharp is implemented in F# and exposes forward and reverse ADoperators as general nestable higher-order functions, usable by any .NETlanguage. It provides high-performance linear algebra primitives---scalars,vectors, and matrices, with a generalization to tensors underway---that arefully supported by all the AD operators, and which use a BLAS/LAPACK backendvia the highly optimized OpenBLAS library. DiffSharp currently uses operatoroverloading, but we are developing a transformation-based version of thelibrary using F#'s "code quotation" metaprogramming facility. Work on aCUDA-based GPU backend is also underway. 041b061a72


Welcome to the group! You can connect with other members, ge...
bottom of page