public void DictionaryStorageTypeContent(RootDefinition root, string storageTypeName, AssetDictionaryAttribute attribute) { var keyType = GetKeyType(root.Root, attribute); var valueType = root.Root; var dictionaryType = typeof(Dictionary <,>).MakeGenericType(keyType, valueType); var proxyDictionaryTypeName = GetTypeName(dictionaryType, true); var originDictionaryTypeName = GetTypeName(dictionaryType, false); GenericFieldDeclaration(proxy, proxyDictionaryTypeName, false, true); GenericFieldDeclaration(cache, originDictionaryTypeName, false, false); var originToProxy = GetTypeConversion(dictionaryType, "value", true); var proxyToOrigin = GetTypeConversion(dictionaryType, proxy, false); const string format = @" public <#=originDictionaryTypeName#> Value { get { <#=cache#> = <#=proxyToOrigin#>; return <#=cache#>; } set { <#=proxy#> = <#=originToProxy#>; <#=cache#> = value; } } public static implicit operator <#=originDictionaryTypeName#>(<#=storageTypeName#> storage) { return storage == null ? default(<#=originDictionaryTypeName#>) : storage.Value; } "; _stringBuilder .AppendLine() .Append(new StringBuilder(format) .Replace("<#=originDictionaryTypeName#>", originDictionaryTypeName) .Replace("<#=storageTypeName#>", storageTypeName) .Replace("<#=cache#>", cache) .Replace("<#=proxy#>", proxy) .Replace("<#=proxyToOrigin#>", proxyToOrigin) .Replace("<#=originToProxy#>", originToProxy)) .AppendLine(); foreach (var pair in DictionaryProxyGenericArguments) { DictionaryType(pair.Key, pair.Value); } }
public void StorageType(RootDefinition root, AssetDeclarationAttributeBase attribute) { var storageTypeName = GetStorageTypeName(root.Root, attribute); var rootTypeName = GetTypeName(root.Root, false); var typeGuid = Guid.NewGuid().ToString(); var attributeGroup = attribute.Group; const string format = @" [Storage(""<#=attributeGroup#>"", typeof(<#=rootTypeName#>))] [GuidAttribute(""<#=typeGuid#>"")] public sealed class <#=storageTypeName#> : StorageBase { public <#=storageTypeName#>() { Group = ""<#=attributeGroup#>""; } protected override object GetStoredValue() { return Value; } "; _stringBuilder .AppendLine() .Append(new StringBuilder(format) .Replace("<#=attributeGroup#>", attributeGroup) .Replace("<#=rootTypeName#>", rootTypeName) .Replace("<#=storageTypeName#>", storageTypeName) .Replace("<#=typeGuid#>", typeGuid)) .AppendLine(); if (attribute is SingleAssetAttribute) { SingleStorageTypeContent(root, storageTypeName); } else if (attribute is AssetListAttribute) { ListStorageTypeContent(root, storageTypeName); } else if (attribute is AssetDictionaryAttribute) { DictionaryStorageTypeContent(root, storageTypeName, attribute as AssetDictionaryAttribute); } _stringBuilder.AppendLine(" }"); }
private void ListStorageTypeContent(RootDefinition root, string storageTypeName) { var listType = typeof(List <>).MakeGenericType(root.Root); var listOriginTypeName = GetTypeName(listType, false); var listProxyTypeName = GetTypeName(listType, true); GenericFieldDeclaration(proxy, listProxyTypeName, false, true); GenericFieldDeclaration(cache, listOriginTypeName, false, false); var proxyToOrigin = GetTypeConversion(listType, proxy, false); var originToProxy = GetTypeConversion(listType, "value", true); const string format = @" public <#=listOriginTypeName#> Value { get { <#=cache#> = <#=proxyToOrigin#>; return <#=cache#>; } set { <#=proxy#> = <#=originToProxy#>; <#=cache#> = value; } } public static implicit operator <#=listOriginTypeName#>(<#=storageTypeName#> storage) { return storage == null ? default(<#=listOriginTypeName#>) : storage.Value; } "; _stringBuilder .AppendLine() .Append(new StringBuilder(format) .Replace("<#=proxy#>", proxy) .Replace("<#=cache#>", cache) .Replace("<#=proxyToOrigin#>", proxyToOrigin) .Replace("<#=originToProxy#>", originToProxy) .Replace("<#=listOriginTypeName#>", listOriginTypeName) .Replace("<#=storageTypeName#>", storageTypeName)) .AppendLine(); foreach (var listTypeName in ListProxyGenericArguments) { ListType(listTypeName); } }
private void SingleStorageTypeContent(RootDefinition root, string storageTypeName) { var originTypeName = GetTypeName(root.Root, false); var proxyTypeName = GetGluedTypeName(GetProxyTypeName(root.Root)); GenericFieldDeclaration(proxy, proxyTypeName, false, true); GenericFieldDeclaration(cache, originTypeName, false, false); const string format = @" public <#=originTypeName#> Value { get { <#=cache#> = <#=proxy#>; return <#=cache#>; } set { <#=proxy#> = value; <#=cache#> = value; } } public static implicit operator <#=originTypeName#>(<#=storageTypeName#> storage) { return storage == null ? default(<#=originTypeName#>) : storage.Value; } "; _stringBuilder .AppendLine() .Append(new StringBuilder(format) .Replace("<#=proxy#>", proxy) .Replace("<#=cache#>", cache) .Replace("<#=originTypeName#>", originTypeName) .Replace("<#=storageTypeName#>", storageTypeName)) .AppendLine(); }