Skip to content

Instantly share code, notes, and snippets.

@jacobmendoza
Created October 26, 2012 08:43
Show Gist options
  • Save jacobmendoza/3957681 to your computer and use it in GitHub Desktop.
Save jacobmendoza/3957681 to your computer and use it in GitHub Desktop.
El motor de plantillas más simple del mundo
private string ObjectToTextTemplate(string text, object objectToSerialize)
{
const string enclosingSymbol = "#";
const string entityProxyModule = "EntityProxyModule";
var type = objectToSerialize.GetType();
var isEntityFrameworkProxyObject = type.Module.ScopeName.Contains(entityProxyModule);
foreach (var propertyInfo in type.GetProperties())
{
if ((!propertyInfo.CanRead) || (!propertyInfo.CanWrite)) continue;
if (type.BaseType != null)
{
var typeName = (isEntityFrameworkProxyObject) ? type.BaseType.Name : type.Name;
var tokenToLookup = string.Format("{0}{1}.{2}{0}", enclosingSymbol, typeName, propertyInfo.Name);
text = text.Replace(tokenToLookup, propertyInfo.GetValue(objectToSerialize, null).ToString());
}
}
return text;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment