Help with Custom Scaffold

Feb 24, 2012 at 9:41 PM

This is more of a T4 template writing question, but I'm trying to write a custom t4 template to be called by a custom scaffold and I can't figure out how to determine if the type of the property is IEnumerable<T> and what type T is. If I were using straight Reflection, it would be relatively straight forward, but I'm a bit thrown by the CodeProperty and CodeType classes.

 

Clearly in my example below "modelProperty.Type.UnderlyingTypeIs<IList<>>()" is wrong, but I'm not sure what the right approach is.


			<# foreach (var modelProperty in modelVisibleProperties) { #>
				<# if (modelProperty.Type.UnderlyingTypeIs<int>() || modelProperty.Type.UnderlyingTypeIs<decimal>() || modelProperty.Type.UnderlyingTypeIs<double>()) { #>
					<#= modelVariable #>.<#= modelProperty.Name #> = 12;
				<# } else if (modelProperty.Type.UnderlyingTypeIs<DateTime>()) { #>
					<#= modelVariable #>.<#= modelProperty.Name #> = DateTime.Now;
				<# } else if (modelProperty.Type.UnderlyingTypeIs<IList<>>()) { #>
					<#= modelVariable #>.<#= modelProperty.Name #> = new [5];
				<# } else { #>
					<#= modelVariable #>.<#= modelProperty.Name #> = "Hyjinx looks funny";
				<# } #>
			<# } #>


 

 

Can someone point me in the right direction?

Apr 19, 2012 at 10:17 PM

You must get the "Kind" from the property.


 

# Gets the Kind of the member.

 # http://msdn.microsoft.com/en-us/library/envdte.vscmelement.aspx

 $memberKind = $member.Kind

 


 

You get the members from the class object.

$classModelType = Get-ProjectType $MyClassClass


loop through the members with

foreach($member in $classModelType .Members) 


Verify that the $member is a property.

# if the member is a property

if($memberKind -eq 4)


verify that the type is  "IEnumerable<T>"

if($member.Type.AsString -eq "IEnumerable<T>")

 

Checking for IEnumerable<T> may not be exactly what you need but Write-Host what the $member.Type.AsString is and you should see it.

Then check against that.

 

Hope this helps.