private void GenerateCS() { using (Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider()) { CodeGeneratorOptions opts = new CodeGeneratorOptions(); StringWriter sw = new StringWriter(); List <CodeTypeReference> implTypes = new List <CodeTypeReference>(); if (_property.ImplementationTypes != null) { var arr = new CodeTypeReference[_property.ImplementationTypes.Count]; _property.ImplementationTypes.CopyTo(arr, 0); _property.ImplementationTypes.Clear(); _property.PrivateImplementationType = null; implTypes.AddRange(arr); } provider.GenerateCodeFromMember(_property, sw, opts); foreach (CodeTypeReference tr in implTypes) { _property.ImplementationTypes.Add(tr); } //StringReader sr = new StringReader(sw.GetStringBuilder().ToString()); //string line = sr.ReadLine(); //while (string.IsNullOrEmpty(line) || line.StartsWith("/") || line.StartsWith("[")) // line = sr.ReadLine(); StringBuilder sb = new StringBuilder(); if (InterfaceProperties != null) { foreach (CodeTypeReference tr in implTypes) { string prop; if (InterfaceProperties.TryGetValue(tr, out prop)) { var newProp = Define.Property(_property.Type, MemberAttributes.Private, prop).Implements(tr); if (_property.HasGet) { newProp.GetStatements.Add(Emit.@return(() => [email protected](_property.Name))); newProp.HasGet = true; } if (_property.HasSet) { newProp.SetStatements.Add(Emit.assignProperty(_property.Name, () => CodeDom.VarRef("value"))); newProp.HasSet = true; } StringWriter swNew = new StringWriter(); provider.GenerateCodeFromMember(CodeDomTreeProcessor.ProcessMember(newProp, CodeDomGenerator.Language.CSharp), swNew, opts); sb.Append(swNew.ToString()); } } if (sb.Length > 0) { sb.Insert(0, Environment.NewLine); } } Text = sw.GetStringBuilder().ToString() + sb.ToString(); } }
/// <summary> /// Reads the WireGuard service configuration from file. /// </summary> /// <param name="filePath">File path of WireGuard service configuration file.</param> /// <returns>Success status of reading from the WireGuard service configuration file.</returns> public bool ReadFromFile(string filePath) { if (!File.Exists(filePath)) { return(false); } var parser = new IniParser.FileIniDataParser(); var iniData = parser.ReadFile(filePath); Interface = new InterfaceProperties() { PrivateKey = iniData["Interface"]["PrivateKey"], Address = iniData["Interface"]["Address"], DNS = iniData["Interface"]["DNS"], }; Peer = new PeerProperties() { PublicKey = iniData["Peer"]["PublicKey"], AllowedIPs = iniData["Peer"]["AllowedIPs"], Endpoint = iniData["Peer"]["Endpoint"], }; return(true); }
/// <summary> /// Initializes a new instance of the <see cref="Config"/> class. /// </summary> /// <param name="privateKey">Private key of the interface.</param> /// <param name="address">Address of the interface.</param> /// <param name="dns">DNS of the interface.</param> public Config(string privateKey, string address, string dns) { Interface = new InterfaceProperties() { PrivateKey = privateKey, Address = address, DNS = dns, }; Peer = new PeerProperties() { PublicKey = string.Empty, AllowedIPs = string.Empty, Endpoint = string.Empty, }; }
private void GenerateVB() { using (Microsoft.VisualBasic.VBCodeProvider provider = new Microsoft.VisualBasic.VBCodeProvider()) { CodeGeneratorOptions opts = new CodeGeneratorOptions(); StringWriter sw = new StringWriter(); List <CodeTypeReference> implTypes = new List <CodeTypeReference>(); if (_property.ImplementationTypes != null) { var arr = new CodeTypeReference[_property.ImplementationTypes.Count]; _property.ImplementationTypes.CopyTo(arr, 0); _property.ImplementationTypes.Clear(); implTypes.AddRange(arr); } provider.GenerateCodeFromMember(_property, sw, opts); foreach (CodeTypeReference tr in implTypes) { _property.ImplementationTypes.Add(tr); } StringReader sr = new StringReader(sw.GetStringBuilder().ToString()); string line = sr.ReadLine(); while (string.IsNullOrEmpty(line) || line.StartsWith("'") || line.StartsWith("<")) { line = sr.ReadLine(); } StringBuilder sb = new StringBuilder(); sb.Append(line); if (InterfaceProperties != null) { sb.Append(" Implements "); foreach (CodeTypeReference tr in implTypes) { string prop; if (InterfaceProperties.TryGetValue(tr, out prop)) { sb.Append(provider.GetTypeOutput(tr)).Append(".").Append(prop).Append(", "); } } sb.Length -= 2; } Text = sw.GetStringBuilder().Replace(line, sb.ToString()).ToString(); } }
private void GenerateVB() { using (Microsoft.VisualBasic.VBCodeProvider provider = new Microsoft.VisualBasic.VBCodeProvider()) { CodeGeneratorOptions opts = new CodeGeneratorOptions(); StringWriter sw = new StringWriter(); List <CodeTypeReference> implTypes = new List <CodeTypeReference>(); if (_property.ImplementationTypes != null) { implTypes.AddRange(_property.ImplementationTypes.Cast <CodeTypeReference>().Distinct(new CodeTypeReferenceEqualityComparer())); _property.ImplementationTypes.Clear(); } provider.GenerateCodeFromMember(_property, sw, opts); foreach (CodeTypeReference tr in implTypes) { _property.ImplementationTypes.Add(tr); } StringReader sr = new StringReader(sw.GetStringBuilder().ToString()); string line = sr.ReadLine(); while (string.IsNullOrEmpty(line) || line.StartsWith("'") || line.StartsWith("<")) { line = sr.ReadLine(); } StringBuilder sb = new StringBuilder(); sb.Append(line); if (InterfaceProperties != null) { sb.Append(" Implements "); foreach (CodeTypeReference tr in implTypes) { foreach (var prop in InterfaceProperties.Where(it => it.Item1.IsEquals(tr))) { sb.Append(provider.GetTypeOutput(tr)).Append(".").Append(prop.Item2).Append(", "); } } sb.Length -= 2; } Text = sw.GetStringBuilder().Replace(line, sb.ToString()).ToString(); } }