/// <summary> /// Creates a strongly typed resource class for a ResoureSet from the DbResourceManager. /// /// Note: Uses the default ResourceProvider settings as set in the DbResourceConfiguration.Current /// property for opening the database and otherwise setting values. /// </summary> /// <param name="ResourceSetName">The name of the resource set. Should be a GLOBAL resource</param> /// <param name="Namespace">The namespace for the generated class. Null or string.Empty to not generate a namespace</param> /// <param name="Classname">Name of the class to be generated</param> /// <param name="FileName">Output filename for the CSharp class. If null no file is generated and only the class is returned</param> /// <returns>string of the generated class</returns> public string CreateClassFromDatabaseResource(string ResourceSetName, string Namespace, string Classname, string FileName) { // Use the custom ResourceManage to retrieve a ResourceSet DbResourceManager Man = new DbResourceManager(ResourceSetName); ResourceSet ResourceSet = Man.GetResourceSet(CultureInfo.InvariantCulture, false, false); return(CreateClassFromResourceSet(ResourceSet, Namespace, Classname, FileName)); }
/// <summary> /// Generates a strongly typed assembly from the resources /// /// UNDER CONSTRUCTION. /// Doesn't work correctly for Web forms due to hard coded resource managers. /// </summary> /// <param name="ResourceSetName"></param> /// <param name="Namespace"></param> /// <param name="Classname"></param> /// <param name="FileName"></param> /// <returns></returns> public bool CreateStronglyTypedResource(string ResourceSetName, string Namespace, string Classname, string FileName) { try { //wwDbResourceDataManager Data = new wwDbResourceDataManager(); //IDictionary ResourceSet = Data.GetResourceSet("", ResourceSetName); // Use the custom ResourceManage to retrieve a ResourceSet DbResourceManager Man = new DbResourceManager(ResourceSetName); ResourceSet rs = Man.GetResourceSet(CultureInfo.InvariantCulture, false, false); IDictionaryEnumerator Enumerator = rs.GetEnumerator(); // We have to turn into a concret Dictionary Dictionary <string, object> Resources = new Dictionary <string, object>(); while (Enumerator.MoveNext()) { DictionaryEntry Item = (DictionaryEntry)Enumerator.Current; Resources.Add(Item.Key as string, Item.Value); } string[] UnmatchedElements; CodeDomProvider CodeProvider = null; string FileExtension = Path.GetExtension(FileName).TrimStart('.').ToLower(); if (FileExtension == "cs") { CodeProvider = new Microsoft.CSharp.CSharpCodeProvider(); } else if (FileExtension == "vb") { CodeProvider = new Microsoft.VisualBasic.VBCodeProvider(); } CodeCompileUnit Code = StronglyTypedResourceBuilder.Create(Resources, ResourceSetName, Namespace, CodeProvider, false, out UnmatchedElements); StreamWriter writer = new StreamWriter(FileName); CodeProvider.GenerateCodeFromCompileUnit(Code, writer, new CodeGeneratorOptions()); writer.Close(); } catch (Exception ex) { this.ErrorMessage = ex.Message; return(false); } return(true); }
/// <summary> /// Creates a strongly typed resource from a ResourceSet object. The ResourceSet passed should always /// be the invariant resourceset that contains all the ResourceIds. /// /// Creates strongly typed keys for each of the keys/values. /// </summary> /// <param name="resourceSet"></param> /// <param name="Namespace">Namespace of the generated class. Pass Null or string.Empty to not generate a namespace</param> /// <param name="classname">Name of the class to generate. Pass null to use the ResourceSet name</param> /// <param name="fileName">Output filename for the CSharp class. If null no file is generated and only the class is returned</param> /// <returns></returns> public string CreateResxDesignerClassFromResourceSet(string resourceSetName, string nameSpace, string classname, string fileName) { // Use the custom ResourceManage to retrieve a ResourceSet var man = new DbResourceManager(resourceSetName); var resourceSet = man.GetResourceSet(CultureInfo.InvariantCulture, false, false); IsVb = IsFileVb(fileName); StringBuilder sbClass = new StringBuilder(); CreateResxDesignerClassHeader(classname, nameSpace, IsVb, sbClass); string indent = "\t\t"; // Any resource set that contains a '.' is considered a Local Resource bool IsGlobalResource = !classname.Contains("."); // We'll enumerate through the Recordset to get all the resources IDictionaryEnumerator Enumerator = resourceSet.GetEnumerator(); // We have to turn into a concrete Dictionary while (Enumerator.MoveNext()) { DictionaryEntry item = (DictionaryEntry)Enumerator.Current; if (item.Value == null) item.Value = string.Empty; string typeName = item.Value.GetType().FullName; string key = item.Key as string; if (string.IsNullOrEmpty(key)) continue; string varName = SafeVarName(key); // It's a string if (!IsVb) { sbClass.AppendLine(indent + "public static " + typeName + " " + varName + "\r\n" + indent + "{"); sbClass.AppendFormat(indent + "\tget\r\n" + indent + "\t{{\r\n" + indent + "\t\t" + (string.IsNullOrEmpty(typeName) || typeName == "System.String" ? "return ResourceManager.GetString(\"{0}\", resourceCulture);\r\n" : "return ({1})ResourceManager.GetObject(\"{0}\", resourceCulture);\r\n") + indent + "\t}}\r\n", key,typeName); sbClass.AppendLine(indent + "}\r\n"); } else { sbClass.Append(indent + "Public Shared Property " + varName + "() as " + typeName + "\r\n"); sbClass.AppendFormat(indent + "\tGet\r\n" + indent + "\t\treturn CType( ResourceManager.GetObject(\"{1}\",resourceCulture)\r\n", classname, key,typeName); sbClass.Append(indent + "\tEnd Get\r\n"); sbClass.Append(indent + "End Property\r\n\r\n"); } } if (!IsVb) sbClass.Append("\t}\r\n\r\n"); else sbClass.Append("End Class\r\n\r\n"); string Output = CreateNameSpaceWrapper(nameSpace,IsVb,sbClass.ToString() ); if (!string.IsNullOrEmpty(fileName)) { File.WriteAllText(fileName, Output); return Output; } return sbClass.ToString(); }
/// <summary> /// Creates a strongly typed resource class for a ResoureSet from the DbResourceManager. /// /// Note: Uses the default ResourceProvider settings as set in the DbResourceConfiguration.Current /// property for opening the database and otherwise setting values. /// </summary> /// <param name="ResourceSetName">The name of the resource set. Should be a GLOBAL resource</param> /// <param name="Namespace">The namespace for the generated class. Null or string.Empty to not generate a namespace</param> /// <param name="Classname">Name of the class to be generated</param> /// <param name="FileName">Output filename for the CSharp class. If null no file is generated and only the class is returned</param> /// <returns>string of the generated class</returns> public string CreateClassFromDatabaseResource(string ResourceSetName, string Namespace, string Classname, string FileName) { // Use the custom ResourceManage to retrieve a ResourceSet DbResourceManager Man = new DbResourceManager(ResourceSetName); ResourceSet ResourceSet = Man.GetResourceSet(CultureInfo.InvariantCulture, false, false); return CreateClassFromResourceSet(ResourceSet, Namespace, Classname, FileName); }
/// <summary> /// Generates a strongly typed assembly from the resources /// /// UNDER CONSTRUCTION. /// Doesn't work correctly for Web forms due to hard coded resource managers. /// </summary> /// <param name="ResourceSetName"></param> /// <param name="Namespace"></param> /// <param name="Classname"></param> /// <param name="FileName"></param> /// <returns></returns> public bool CreateStronglyTypedResource(string ResourceSetName,string Namespace, string Classname, string FileName) { try { //wwDbResourceDataManager Data = new wwDbResourceDataManager(); //IDictionary ResourceSet = Data.GetResourceSet("", ResourceSetName); // Use the custom ResourceManage to retrieve a ResourceSet DbResourceManager Man = new DbResourceManager(ResourceSetName); ResourceSet rs = Man.GetResourceSet(CultureInfo.InvariantCulture, false, false); IDictionaryEnumerator Enumerator = rs.GetEnumerator(); // We have to turn into a concret Dictionary Dictionary<string, object> Resources = new Dictionary<string, object>(); while (Enumerator.MoveNext()) { DictionaryEntry Item = (DictionaryEntry) Enumerator.Current; Resources.Add(Item.Key as string, Item.Value); } string[] UnmatchedElements; CodeDomProvider CodeProvider = null; string FileExtension = Path.GetExtension(FileName).TrimStart('.').ToLower(); if (FileExtension == "cs") CodeProvider = new Microsoft.CSharp.CSharpCodeProvider(); else if(FileExtension == "vb") CodeProvider = new Microsoft.VisualBasic.VBCodeProvider(); CodeCompileUnit Code = StronglyTypedResourceBuilder.Create(Resources, ResourceSetName, Namespace, CodeProvider, false, out UnmatchedElements); StreamWriter writer = new StreamWriter(FileName); CodeProvider.GenerateCodeFromCompileUnit(Code, writer, new CodeGeneratorOptions()); writer.Close(); } catch (Exception ex) { this.ErrorMessage = ex.Message; return false; } return true; }
/// <summary> /// Creates a strongly typed resource from a ResourceSet object. The ResourceSet passed should always /// be the invariant resourceset that contains all the ResourceIds. /// /// Creates strongly typed keys for each of the keys/values. /// </summary> /// <param name="resourceSetName"></param> /// <param name="nameSpace">Namespace of the generated class. Pass Null or string.Empty to not generate a namespace</param> /// <param name="classname">Name of the class to generate. Pass null to use the ResourceSet name</param> /// <param name="fileName">Output filename for the CSharp class. If null no file is generated and only the class is returned</param> /// <returns></returns> public string CreateResxDesignerClassFromResourceSet(string resourceSetName, string nameSpace, string classname, string fileName) { classname = SafeClassName(classname); // Use the custom ResourceManage to retrieve a ResourceSet var man = new DbResourceManager(resourceSetName); var resourceSet = man.GetResourceSet(CultureInfo.InvariantCulture, false, false); IsVb = IsFileVb(fileName); StringBuilder sbClass = new StringBuilder(); CreateResxDesignerClassHeader(classname, nameSpace, IsVb, sbClass); string indent = "\t\t"; // We'll enumerate through the Recordset to get all the resources IDictionaryEnumerator enumerator = resourceSet.GetEnumerator(); // We have to turn into a concrete Dictionary while (enumerator.MoveNext()) { DictionaryEntry item = (DictionaryEntry)enumerator.Current; if (item.Value == null) { item.Value = string.Empty; } string typeName = item.Value.GetType().FullName; string key = item.Key as string; if (string.IsNullOrEmpty(key)) { continue; } string varName = SafeVarName(key); // It's a string if (!IsVb) { sbClass.AppendLine(indent + "public static " + typeName + " " + varName + "\r\n" + indent + "{"); sbClass.AppendFormat(indent + "\tget\r\n" + indent + "\t{{\r\n" + indent + "\t\t" + (string.IsNullOrEmpty(typeName) || typeName == "System.String" ? "return ResourceManager.GetString(\"{0}\", resourceCulture);\r\n" : "return ({1})ResourceManager.GetObject(\"{0}\", resourceCulture);\r\n") + indent + "\t}}\r\n", key, typeName); sbClass.AppendLine(indent + "}\r\n"); } else { sbClass.Append(indent + "Public Shared Property " + varName + "() as " + typeName + "\r\n"); sbClass.AppendFormat(indent + "\tGet\r\n" + indent + "\t\treturn CType( ResourceManager.GetObject(\"{1}\",resourceCulture)\r\n", classname, key, typeName); sbClass.Append(indent + "\tEnd Get\r\n"); sbClass.Append(indent + "End Property\r\n\r\n"); } } if (!IsVb) { sbClass.Append("\t}\r\n\r\n"); } else { sbClass.Append("End Class\r\n\r\n"); } string Output = CreateNameSpaceWrapper(nameSpace, IsVb, sbClass.ToString(), false); if (!string.IsNullOrEmpty(fileName)) { File.WriteAllText(fileName + ".designer" + (IsVb ? ".vb" : ".cs"), Output); return(Output); } return(sbClass.ToString()); }