Files
MicrosoftIris/UIX/Microsoft/Iris/Markup/MarkupDataMapping.cs
T

113 lines
3.9 KiB
C#
Raw Normal View History

2022-03-03 08:14:05 -06:00
// Decompiled with JetBrains decompiler
// Type: Microsoft.Iris.Markup.MarkupDataMapping
// Assembly: UIX, Version=4.8.0.0, Culture=neutral, PublicKeyToken=ddd0da4d3e678217
// MVID: A56C6C9D-B7F6-46A9-8BDE-B3D9B8D60B11
// Assembly location: C:\Program Files\Zune\UIX.dll
2022-06-12 23:13:27 -05:00
using System;
using System.Collections.Generic;
2022-06-12 21:38:09 -05:00
using System.Text;
2022-06-12 21:21:29 -05:00
2022-03-03 08:14:05 -06:00
namespace Microsoft.Iris.Markup
{
internal class MarkupDataMapping
{
private string _name;
private MarkupDataMappingEntry[] _mappings;
private MarkupDataTypeSchema _targetType;
private string _provider;
private object _assemblyDataProviderCookie;
public MarkupDataMapping(string name) => _name = name;
public MarkupDataTypeSchema TargetType
{
get => _targetType;
set => _targetType = value;
}
public string Provider
{
get => _provider;
set => _provider = value;
}
public MarkupDataMappingEntry[] Mappings
{
get => _mappings;
set => _mappings = value;
}
public object AssemblyDataProviderCookie
{
get => _assemblyDataProviderCookie;
set => _assemblyDataProviderCookie = value;
}
2022-06-12 21:21:29 -05:00
public override string ToString() => $"({_provider}, {_targetType})";
/// <summary>
/// Generates C# code for a plain-old CLR object (POCO) that can
/// be used to serialize and deserialize the data this mapping
/// represents.
/// </summary>
public string GenerateModelCode()
{
2022-06-12 21:38:09 -05:00
const string indent = " ";
const string newline = "\r\n";
2022-06-12 21:21:29 -05:00
2022-06-12 23:13:27 -05:00
string start = $@"using System;{newline}using System.Collections.Generic;{newline}{newline}namespace Zune.Xml;{newline}{newline}public class {TargetType.Name}{newline}{{{newline}";
2022-06-12 21:38:09 -05:00
string end = $@"}}{newline}";
2022-06-12 21:21:29 -05:00
2022-06-12 21:38:09 -05:00
var sb = new StringBuilder(start);
2022-06-12 21:21:29 -05:00
foreach (var mapping in Mappings)
{
2022-06-12 21:38:09 -05:00
string propertyType = mapping.Property.PropertyType.Name;
2022-06-12 23:13:27 -05:00
if (_knownTypeNames.TryGetValue(propertyType, out var knownTypeName))
propertyType = knownTypeName;
string properyDeclaration = $"{indent}public {propertyType} {mapping.Property.Name} {{ get; set; }}";
2022-06-12 21:21:29 -05:00
if (mapping.Source != null)
{
2022-06-12 23:13:27 -05:00
string source = mapping.Source.StartsWith("/") ? mapping.Source.Substring(1) : mapping.Source;
2022-06-12 21:40:52 -05:00
string xmlElemAttribute = $"{indent}[XmlElement(\"{source}\")]{newline}";
2022-06-12 21:38:09 -05:00
properyDeclaration = xmlElemAttribute + properyDeclaration;
2022-06-12 21:21:29 -05:00
}
2022-06-12 23:13:27 -05:00
Type runtimeType = mapping.Property?.PropertyType?.RuntimeType;
if (runtimeType != null)
{
object defaultValue = (runtimeType.IsValueType && Nullable.GetUnderlyingType(runtimeType) == null)
? Activator.CreateInstance(runtimeType)
: null;
string defaultValueStr = defaultValue?.ToString() ?? "null";
if (defaultValueStr != (mapping.DefaultValue?.ToString() ?? "null"))
properyDeclaration += $" = {defaultValueStr};";
}
sb.Append(properyDeclaration + newline + newline);
2022-06-12 21:21:29 -05:00
}
2022-06-12 21:38:09 -05:00
sb.Append(end);
2022-06-12 21:21:29 -05:00
2022-06-12 21:38:09 -05:00
return sb.ToString();
2022-06-12 21:21:29 -05:00
}
2022-06-12 23:13:27 -05:00
private static readonly Dictionary<string, string> _knownTypeNames = new Dictionary<string, string>()
{
{ "Boolean", "bool" },
{ "Int32", "int" },
{ "UInt32", "uint" },
{ "Int64", "long" },
{ "UInt64", "ulong" },
{ "Int16", "short" },
{ "UInt16", "ushort" },
{ "String", "string" },
{ "List", "List<object>" },
};
2022-03-03 08:14:05 -06:00
}
}