Click here to Skip to main content
15,888,454 members
Home / Discussions / C#
   

C#

 
GeneralRe: Semantic Types for Name-Strings Pin
Sascha Lefèvre11-Feb-16 5:00
professionalSascha Lefèvre11-Feb-16 5:00 
AnswerRe: Semantic Types for Name-Strings Pin
Gerry Schmitz11-Feb-16 4:52
mveGerry Schmitz11-Feb-16 4:52 
GeneralRe: Semantic Types for Name-Strings Pin
Sascha Lefèvre11-Feb-16 5:07
professionalSascha Lefèvre11-Feb-16 5:07 
GeneralRe: Semantic Types for Name-Strings Pin
Gerry Schmitz11-Feb-16 7:29
mveGerry Schmitz11-Feb-16 7:29 
AnswerRe: Semantic Types for Name-Strings Pin
Midi_Mick11-Feb-16 11:02
professionalMidi_Mick11-Feb-16 11:02 
AnswerRe: Semantic Types for Name-Strings Pin
BillWoodruff15-Feb-16 4:59
professionalBillWoodruff15-Feb-16 4:59 
GeneralRe: Semantic Types for Name-Strings Pin
Sascha Lefèvre15-Feb-16 5:16
professionalSascha Lefèvre15-Feb-16 5:16 
GeneralRe: Semantic Types for Name-Strings Pin
BillWoodruff15-Feb-16 8:36
professionalBillWoodruff15-Feb-16 8:36 
Hi Sascha, I'm in the process of studying T4 in depth (steep learning curve ... for me). But, I can see the power available by using it. fyi: here's an experimental design-time .tt file that will produce a new .cs file with a series of "nested" Classes built from iterating a List of Strings. Each Class except the first in the List will inherit from the previous Class in the List, and each Class will define a Property to hold a List of the Class that inherits from it ... except for the last Class in the List which has no sub-Class. An interesting experiment, but don't me ask me what it might be good for Smile | :)

The design-time .tt file:
C#
<#@ template  debug="true" hostSpecific="true"#>
<#@ output extension=".cs"#>
<#@ Assembly Name="System.Core"#>
<#@ Assembly Name="System.Windows.Forms"#>
<#@ import namespace="System"#>
<#@ import namespace="System.IO"#>
<#@ import namespace="System.Diagnostics"#>
<#@ import namespace="System.Linq"#>
<#@ import namespace="System.Collections"#>
<#@ import namespace="System.Collections.Generic"#>
<#
List<string> categories = new List<string>{"Life","Domain", "Kingdom", "Phylum", "Class", "Order", "Family", "Genus", "Species"}; 
#>
using System;
using System.Collections.Generic;
using System.Linq;
/* 
	Bill Woodruff
	T4 Experiement #2 
*/
namespace TestNameSpace
{
	public class Life
	{
		public string Name { set; get; }
		public List<Domain> Domains{ set; get; }

		public Life(string name = "Life", params string[] args)
		{
			Name = name;

			Domains = new List<Domain>();

			foreach(string str in args) Domains.Add(new Domain(str));
		}

		<#
		for (int i = 1; i < categories.Count; i++)
		{
			#>public class <#=categories[i]#>: <#=categories[i - 1]#>
			{

			<#if(i < categories.Count - 1)
			{	
			#>
			public List< <#=categories[i + 1]#>> <#=categories[i + 1]#>s = new List< <#=categories[i + 1]#>>();  
			<#}#>

			public <#=categories[i]#>(string name = "", params string[] args)
			{
				Name = name;
				<#if(i < categories.Count - 1)
				{
				#>	
				foreach(string str in args)
				{
					<#=categories[i +1]#>s.Add(new <#=categories[i + 1]#>(str));
				}
	<#}#>
		}
		}

	<#}
#>
}
}
When processed by the compiler's TextTemplatingTool, this generates the following "live" class structure:
C#
using System;
using System.Collections.Generic;
using System.Linq;
/* 
	Bill Woodruff
	T4 Experiement #2 
*/

namespace TestNameSpace
{
    public class Life
    {
        public Life(string name = "Life", params string[] args)
        {
            Name = name;

            Domains = new List<Domain>();

            foreach (string str in args) Domains.Add(new Domain(str));
        }

        public string Name { set; get; }
        public List<Domain> Domains { set; get; }

        public class Domain : Life
        {
            public List<Kingdom> Kingdoms = new List<Kingdom>();

            public Domain(string name = "", params string[] args)
            {
                Name = name;

                foreach (string str in args)
                {
                    Kingdoms.Add(new Kingdom(str));
                }
            }
        }

        public class Kingdom : Domain
        {
            public List<Phylum> Phylums = new List<Phylum>();

            public Kingdom(string name = "", params string[] args)
            {
                Name = name;

                foreach (string str in args)
                {
                    Phylums.Add(new Phylum(str));
                }
            }
        }

        public class Phylum : Kingdom
        {
            public List<Class> Classs = new List<Class>();

            public Phylum(string name = "", params string[] args)
            {
                Name = name;

                foreach (string str in args)
                {
                    Classs.Add(new Class(str));
                }
            }
        }

        public class Class : Phylum
        {
            public List<Order> Orders = new List<Order>();

            public Class(string name = "", params string[] args)
            {
                Name = name;

                foreach (string str in args)
                {
                    Orders.Add(new Order(str));
                }
            }
        }

        public class Order : Class
        {
            public List<Family> Familys = new List<Family>();

            public Order(string name = "", params string[] args)
            {
                Name = name;

                foreach (string str in args)
                {
                    Familys.Add(new Family(str));
                }
            }
        }

        public class Family : Order
        {
            public List<Genus> Genuss = new List<Genus>();

            public Family(string name = "", params string[] args)
            {
                Name = name;

                foreach (string str in args)
                {
                    Genuss.Add(new Genus(str));
                }
            }
        }

        public class Genus : Family
        {
            public List<Species> Speciess = new List<Species>();

            public Genus(string name = "", params string[] args)
            {
                Name = name;

                foreach (string str in args)
                {
                    Speciess.Add(new Species(str));
                }
            }
        }

        public class Species : Genus
        {
            public Species(string name = "", params string[] args)
            {
                Name = name;
            }
        }
    }
}

«In art as in science there is no delight without the detail ... Let me repeat that unless these are thoroughly understood and remembered, all “general ideas” (so easily acquired, so profitably resold) must necessarily remain but worn passports allowing their bearers short cuts from one area of ignorance to another.» Vladimir Nabokov, commentary on translation of “Eugene Onegin.”


modified 15-Feb-16 14:43pm.

GeneralRe: Semantic Types for Name-Strings Pin
Sascha Lefèvre15-Feb-16 9:08
professionalSascha Lefèvre15-Feb-16 9:08 
QuestionHow to reset scannersettings using .net code Pin
Member 1125820311-Feb-16 1:09
Member 1125820311-Feb-16 1:09 
AnswerRe: How to reset scannersettings using .net code Pin
OriginalGriff11-Feb-16 1:37
mveOriginalGriff11-Feb-16 1:37 
Questionavoid blinking screen+window form application c# Pin
ginsa vaheed10-Feb-16 23:31
ginsa vaheed10-Feb-16 23:31 
AnswerRe: avoid blinking screen+window form application c# Pin
Pete O'Hanlon10-Feb-16 23:34
mvePete O'Hanlon10-Feb-16 23:34 
AnswerRe: avoid blinking screen+window form application c# Pin
OriginalGriff11-Feb-16 0:34
mveOriginalGriff11-Feb-16 0:34 
AnswerRe: avoid blinking screen+window form application c# Pin
Richard MacCutchan11-Feb-16 1:35
mveRichard MacCutchan11-Feb-16 1:35 
AnswerRe: avoid blinking screen+window form application c# Pin
RugbyLeague11-Feb-16 3:38
RugbyLeague11-Feb-16 3:38 
AnswerRe: avoid blinking screen+window form application c# Pin
Gerry Schmitz11-Feb-16 5:04
mveGerry Schmitz11-Feb-16 5:04 
QuestionHow to implement class /component Versioning using abstract class in c# Pin
Tridip Bhattacharjee10-Feb-16 21:51
professionalTridip Bhattacharjee10-Feb-16 21:51 
AnswerRe: How to implement class /component Versioning using abstract class in c# Pin
Richard MacCutchan10-Feb-16 22:03
mveRichard MacCutchan10-Feb-16 22:03 
GeneralRe: How to implement class /component Versioning using abstract class in c# Pin
Tridip Bhattacharjee11-Feb-16 21:50
professionalTridip Bhattacharjee11-Feb-16 21:50 
GeneralRe: How to implement class /component Versioning using abstract class in c# Pin
Richard MacCutchan11-Feb-16 22:14
mveRichard MacCutchan11-Feb-16 22:14 
QuestionHow to red variable continuously Pin
Zefir110-Feb-16 20:34
Zefir110-Feb-16 20:34 
AnswerRe: How to red variable continuously Pin
OriginalGriff10-Feb-16 20:50
mveOriginalGriff10-Feb-16 20:50 
GeneralRe: How to red variable continuously Pin
Zefir110-Feb-16 21:20
Zefir110-Feb-16 21:20 
GeneralRe: How to red variable continuously Pin
OriginalGriff10-Feb-16 21:38
mveOriginalGriff10-Feb-16 21:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.