Skip to content

Instantly share code, notes, and snippets.

@robfe
Created October 26, 2010 21:46
Show Gist options
  • Save robfe/647885 to your computer and use it in GitHub Desktop.
Save robfe/647885 to your computer and use it in GitHub Desktop.
Reflects over a class and generates an interface, and an interface wrapper for your class
<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ output extension="Generated.cs" #>
<#@ assembly name="System.Core" #>
<#/* Modify the following assembly reference(s) to include the classes you are interested in: */#>
<#@ assembly name="c:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\Silverlight\v4.0\Profile\WindowsPhone\System.Xml.dll" #>
<#@ import namespace="System.Reflection" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Runtime.Remoting.Messaging" #>
<#
/* Modify the following types to include the classes you are interested in: */
Type[] types = {
typeof(System.Xml.XmlWriter),
typeof(System.Xml.XmlReader),
};
var flags = BindingFlags.FlattenHierarchy | BindingFlags.Instance | BindingFlags.Public;
Func<MethodInfo, bool> methodFilter = x => !x.IsSpecialName
&& x.DeclaringType != typeof(object)
&& !"Equals,GetHashCode".Split(',').Contains(x.Name);
#>
namespace <#=CallContext.LogicalGetData("NamespaceHint") #>
{
<#
foreach(Type t in types)
{
var interfaceNames = t.GetInterfaces().Select(x=>TypeDef(x)).ToArray();
string interfaces = interfaceNames.Length == 0 ? "" : " : "+string.Join(", ", interfaceNames);
#>
public interface I<#=t.Name + interfaces#>
{
<#
foreach(MethodInfo m in t.GetMethods(flags).Where(methodFilter))
{
#>
<#= TypeDef(m.ReturnType) #> <#= m.Name #>(<#= FormalParameters(m) #>);
<#
}
foreach(EventInfo e in t.GetEvents(flags).Where(x=>!x.IsSpecialName))
{
#>
event <#= TypeDef(e.EventHandlerType) #> <#= e.Name #>;
<#
}
foreach(PropertyInfo p in t.GetProperties(flags).Where(x=>!x.IsSpecialName && x.GetIndexParameters() == null))
{
#>
<#= TypeDef(p.PropertyType) #> <#= p.Name #> { <# if (p.GetGetMethod() != null) { #>get; <# } if (p.GetSetMethod() != null) { #>set; <# } #>}
<#
}
#>
}
public class <#=t.Name#>Wrapper : I<#=t.Name#>
{
readonly <#=t.FullName#> wrappedObject;
public <#=t.Name#>Wrapper(<#=t.FullName#> wrappedObject)
{
this.wrappedObject = wrappedObject;
}
<#
foreach(MethodInfo m in t.GetMethods(flags).Where(methodFilter))
{
#>
public <#= TypeDef(m.ReturnType) #> <#= m.Name #>(<#= FormalParameters(m) #>)
{
<# if (m.ReturnType != typeof(void)) { #>return <# } #>wrappedObject.<#= m.Name #>(<#= ParameterNames(m) #>);
}
<#
}
foreach(EventInfo e in t.GetEvents(flags).Where(x=>!x.IsSpecialName))
{
#>
public event <#= TypeDef(e.EventHandlerType) #> <#= e.Name #>
{
add
{
wrappedObject.<#= e.Name #> += value;
}
remove
{
wrappedObject.<#= e.Name #> -= value;
}
}
<#
}
foreach(PropertyInfo p in t.GetProperties(flags).Where(x=>!x.IsSpecialName && x.GetIndexParameters() == null))
{
#>
public <#= TypeDef(p.PropertyType) #> <#= p.Name #>
{
<#
if (p.GetGetMethod() != null)
{
#>
get
{
return wrappedObject.<#= p.Name #>;
}
<#
}
if (p.GetSetMethod() != null)
{
#>
set
{
wrappedObject.<#= p.Name #> = value;
}
<#}#>
}
<#
}
#>
}
<#
}
#>
}
<#+
public static string TypeDef(Type t)
{
if (t == typeof(void))
return "void";
else if (t.IsGenericType)
{
string name = t.FullName.Split('`')[0];
var args = string.Join(", ", t.GetGenericArguments().Select(TypeDef).ToArray());
return name + "<" + args + ">";
}
return t.FullName;
}
public string FormalParameters(MethodInfo method)
{
var v = method.GetParameters().Select(x => TypeDef(x.ParameterType) + " " + x.Name);
return string.Join(", ", v.ToArray());
}
public string ParameterNames(MethodInfo method)
{
var v = method.GetParameters().Select(x => x.Name);
return string.Join(", ", v.ToArray());
}
#>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment