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 8847001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:07:09+00:00 2026-06-14T12:07:09+00:00

What I want is a class (or a list or whatever) where I can

  • 0

What I want is a class (or a list or whatever) where I can say:

String ClientName;
String DealerID;

And it would generate the code for me like

public static string ClientName
{
    get
    {
        object obj = HttpContext.Current.Session["clientName"];

        if (obj != null)
        {
            return (string)obj;
        }

        return null;
    }

    set
    {
        HttpContext.Current.Session["clientName"] = value;
    }
}

One way may be to use reflections but I don’t know how.
Another solution maybe to use typed datasets but again I don’t know how.
Another Way may be to use T4 templates but I do´t know how.

  • 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-06-14T12:07:10+00:00Added an answer on June 14, 2026 at 12:07 pm

    T4 sample:

    <#
        // Here is the model
        Model = new []
            {
                P ("string", "ClientName"),
                P ("string", "DealerID"),
            };
    #>
    
    <#
        // Here is the "view", this can be extracted into a ttinclude file and reused
    #>
    namespace MyNameSpace
    {
        using System.Web;
    
        partial class SessionState
        {
    <#
        foreach (var propertyDefinition in Model)
        {
    #>
            public static <#=propertyDefinition.Type#> <#=propertyDefinition.Name#>
            {
                get
                {
                    object obj = HttpContext.Current.Session["<#=propertyDefinition.SessionName#>"];
    
                    if (obj != null)
                    {
                        return (<#=propertyDefinition.Type#>)obj;
                    }
    
                    return null;
                }
    
                set
                {
                    HttpContext.Current.Session["<#=propertyDefinition.SessionName#>"] = value;
                }
            }    
    <#
        }
    #>
        }
    }
    <#+
    
        PropertyDefinition[] Model = new PropertyDefinition[0];
    
        class PropertyDefinition
        {
            public string Type;
            public string Name;
    
            public string SessionName
            {
                get
                {
                    var name = Name ?? "";
                    if (name.Length == 0)
                    {
                        return name;
                    }
    
                    return char.ToLower(name[0]) + name.Substring(1);
    
                }
            }
        }
    
        static PropertyDefinition P (string type, string name)
        {
            return new PropertyDefinition
            {
                Type = type ?? "<NoType>",
                Name = name ?? "<NoName>",
            };
        }
    
    #>
    

    It generates the following code:

    namespace MyNameSpace
    {
        using System.Web;
    
        partial class SessionState
        {
                public static string ClientName
            {
                get
                {
                    object obj = HttpContext.Current.Session["clientName"];
    
                    if (obj != null)
                    {
                        return (string)obj;
                    }
    
                    return null;
                }
    
                set
                {
                    HttpContext.Current.Session["clientName"] = value;
                }
            }    
                public static string DealerID
            {
                get
                {
                    object obj = HttpContext.Current.Session["dealerID"];
    
                    if (obj != null)
                    {
                        return (string)obj;
                    }
    
                    return null;
                }
    
                set
                {
                    HttpContext.Current.Session["dealerID"] = value;
                }
            }    
            }
    }
    

    If you do extract the “view” the model file would look like this:

    <#
        // Here is the model
        Model = new []
            {
                P ("string", "ClientName"),
                P ("string", "DealerID"),
            };
    #>
    
    <#@ include file="$(SolutionDir)\GenerateSessionState.ttinclude"#>
    

    Regarding CodeSnippets vs T4

    Sometimes it is thought that CodeSnippets (and Resharper code templates) are equivalent to T4. They are not.

    CodeSnippets (and others) promotes code redundancy and is basically CopyPaste programming with extra tool support.

    T4 (or CodeSmith) are MetaProgramming Tools which helps you reduce code redundancy in the code you maintain (they might generate redundant code but you don’t need to maintain that code).

    A thought experiment around CodeSnippets; you have used a snippet extensively but you realize there’s an issue in the code it generated.

    How do you resolve it? You have to find all instances where you used the snippet and adjust the code but run into problems; how do you find all instances? How do you merge the Changes when someone modified the snippeted code?

    With MetaProgramming Tools like T4 or CodeSmith you fix the template and regenerate the code.

    This is why I die a litte bit inside everytime someone mentions code snippets.

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

Sidebar

Related Questions

I want to do something like this: public class ScadenzaService { ... public List<Scadenza>
I'm having a class with a list of constants. I want something like get_object_vars
I have a class with static members. I want to get a list of
Given a type such as: public class FooList : List<Foo> { public string SomeMessage
I have to store scheduled events, (like say class times, for example) that can
What I want is this behavior: class a: list = [] x = a()
I want to list all users with their corropsonding user class. Here are simplified
I have List of particular class. I want to save this List at a
I want to pass a property list of a class to a function. with
I have 2 list and i want to add a class =last to the

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.