Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6558145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:09:49+00:00 2026-05-25T13:09:49+00:00

Is there any language which has a form of code templating? Let me explain

  • 0

Is there any language which has a form of code templating? Let me explain what I mean… I was working on a C# project today in which one of my classes was very repetitive, a series of properties getters and setters.

    public static int CustomerID
    {
        get
        {
            return SessionHelper.Get<int>("CustomerID", 0); // 0 is the default value
        }
        set
        {
            SessionHelper.Set("CustomerID", value);
        }
    }

    public static int BasketID
    {
        get
        {
            return SessionHelper.Get<int>("BasketID", 0); // 0 is the default value
        }
        set
        {
            SessionHelper.Set("BasketID", value);
        }
    }

... and so forth ...

I realize that this could break down into basically a type, a name, and a default value.

I saw this article, which is similar to what I envision, but has no room for parameters (the default).

Generic Property in C#

But I was thinking, there are many times where code breaks down into templates.

For example, the syntax could go as such:

public template SessionAccessor(obj defaultValue) : static this.type this.name
{
     get
     {
          return SessionHelper.Get<this.type>(this.name.ToString(), defaultValue);
     }
     set
     {
          SessionHelper.Set(this.name.ToString(), value);
     }
}

public int CustomerID(0), BasketID(0) with template SessionAccessor;
public ShoppingCart Cart(new ShoppingCart()) with template SessionAccessor; // Class example

I feel like this would have a lot of possibilities in writing succinct, DRY code. This type of thing would be somewhat achievable in c# with reflection, however that is slow and this should done during the compile.

So, question: Is this type of functionality possible in any existing programming language?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T13:09:50+00:00Added an answer on May 25, 2026 at 1:09 pm

    As Marc Gravell commented, it’s an easy job for T4 (Text Template Transformation Toolkit), which is a templating processor integrated inside Visual Studio, that can be used with C# or VB and can generate anything. It’s a tool, not a built-in language feature though.

    Add a Text Template file (.tt) to your project, the template will be as simple as:

    <#@ template debug="false" hostspecific="false" language="C#" #>
    <#@ output extension=".generated.cs" #>
    <#
    var properties = new[] {
        new Property { Type = typeof(int), Name = "CustomerID", DefaultValue = 0 },
        new Property { Type = typeof(int), Name = "BasketID", DefaultValue = 0 },
    };
    #>
    namespace YourNameSpace {
        public partial class YourClass {
    <# foreach (Property property in properties) { #>
            public static <#= property.Type.FullName #> <#= property.Name #> {
                get { return SessionHelper.Get<<#= property.Type.FullName #>>("<#= property.Name #>", <#= property.DefaultValue #>); }
                set { SessionHelper.Set("<#= property.Name #>", value); }
            }
    <# } #>
        }
    }
    <#+
    public class Property {
        public Type Type { get; set; }
        public string Name { get; set; }
        public object DefaultValue { get; set; }
    }
    #>
    

    T4 are great for this kind of code generation. I highly recommend T4 Toolbox to easily generate multiple files per template, access EnvDTE to parse your existing C# code directly inside Visual Studio and other goodness.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any datatype /container in any language which checks for the uniqueness of
Is there any programming language (or type system) in which you could express the
Are there any open source libraries (any language, python/PHP preferred) that will tokenize/parse an
Is there any way, in any language, to hook my program when a user
Are there any interpreters available for the Android platform for any language at all?
Are there any good language-agnostic distributed make systems for linux that are secure and
If there are any language designers out there (or people simply in the know),
Are there any high level language that don't support using C++ libraries?
I just wondered if there are any libraries in Java or any other language
Is there any way in the SQL language or in MySQL (or other DBMA)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.