/// <summary> /// - /// </summary> /// <param name="newC"></param> /// <param name="e"></param> private void CreateProperty(CodeTypeDeclaration newC, XmlElement e) { string propName = e.GetAttribute("name"); var propType = ResolveType(e.GetAttribute("datatype")); var rel_field_prop_name = createPropertyConstName(newC, e, propName); var new_prop = new CodeMemberProperty { Attributes = MemberAttributes.Public | MemberAttributes.Final, Name = "p_" + propName, HasGet = true, HasSet = true, Type = propType }; var comment = e.GetAttribute("displayname"); if (!string.IsNullOrEmpty(comment)) new_prop.Comments.Add(new CodeCommentStatement("<summary>\n " + comment + "\n </summary>", true)); new_prop.GetStatements.Add(new CodeMethodReturnStatement( new CodeCastExpression(propType, new CodeMethodInvokeExpression( new CodeMethodReferenceExpression(new CodeBaseReferenceExpression(), "GetDataProperty"), new CodeFieldReferenceExpression(null, rel_field_prop_name))))); new_prop.SetStatements.Add( new CodeMethodInvokeExpression( new CodeMethodReferenceExpression(new CodeBaseReferenceExpression(), "SetDataProperty"), new CodeFieldReferenceExpression(null, rel_field_prop_name), new CodePropertySetValueReferenceExpression())); newC.Members.Add(new_prop); }
Several times I used this mechanism for solving particular problems, and came to the conclusion that it is not suitable for my task. After all, I need to be able to quickly slip a piece of code for substitution, and the methods mentioned generate a code based on its structure. Those. To use the built-in tools, you must first write a template translator that will parse the C # code in order to generate it on its basis. Complete nonsense.
// const string CommandPropertyTemplate = @"public static <type> <name> {{ get {{ return PVCommand.<name>.GetCommand(); }} }} "; // var commandsClass = CodeWriter .BeginSource("Commands.cs") .Using("MyApp.Display") .BeginNamespace("MyAppCommands") .AddClass("Commands").Static(); // // var allcmds = System.Enum.GetValues(typeof(PVCommand)).Cast<PVCommand>(); foreach (var cmd in allcmds) { commandsClass.AddBlock<RoutedUICommand>(cmd.ToString(), CommandPropertyTemplate); }
public enum Strings { [Description("MyCoolApp - trial version")] AppTitle, }
// const string ResourceEntry = @" public const <type> <name>Key = ""<name>""; public static <type> <name> {{ get{{ return App.RString(<name>Key); }} }} "; // var stringTable = CodeWriter .BeginSource("Strings.cs") .BeginNamespace("MyApp") .AddClass("StringTable") .Static(); // foreach (var rstring in GetResourceStrings()) { var resourseKey = rstring.ToString(); stringTable.AddBlock<string>(resourseKey, ResourceEntry); } // XAML void StringTableXamlTo(string dir) { var nsDefault = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"; var nsX = "http://schemas.microsoft.com/winfx/2006/xaml"; var nsCore = "clr-namespace:MyApp.Core;assembly=MyApp.Core"; var resourcedict = new XElement(XName.Get("ResourceDictionary", nsDefault), new XAttribute(XName.Get("Uid", nsX), "StringTable"), new XAttribute(XNamespace.Xmlns + "core", nsCore), new XAttribute(XNamespace.Xmlns + "x", nsX)); foreach (var rstring in GetResourceStrings()) { var resourseKey = rstring.ToString(); var resourceValue = EnumHelper.GetDescription(rstring); var resourceDecl = new XElement(XName.Get("StringObject", nsCore), new XAttribute(XName.Get("Uid", nsX), "UID_" + resourseKey), new XAttribute(XName.Get("Key", nsX), resourseKey), new XAttribute("Localization.Attributes", "Value (Readable Modifiable Text)"), new XAttribute("Value", resourceValue) ); resourcedict.Add(resourceDecl); } var xaml = new XDocument(resourcedict); xaml.Save(Path.Combine(dir, "stringtable.xaml")); } }
const string ModelPropertyTemplate = @" <type> _<name>; public <type> <name> {{ get {{ return _<name>; }} set {{ _<name> = value; if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(""<name>"")); }} } ";
Source: https://habr.com/ru/post/141933/
All Articles