@@ -35,7 +35,7 @@ public class CsProject : ITargetProject
3535 public CsProject ( AXSharpProject AXSharpProject )
3636 {
3737 AxSharpProject = AXSharpProject ;
38- ProjectRootNamespace = MakeValidIdentifier ( AXSharpProject . AxProject . ProjectInfo . Name ) ;
38+ ProjectRootNamespace = GetProjectRootNamespace ( ) ;
3939 }
4040
4141 private AXSharpProject AxSharpProject { get ; }
@@ -97,6 +97,35 @@ private static string GetExecutingAssemblyPath()
9797 return assemblyPath ! ;
9898 }
9999
100+ /// <summary>
101+ /// Gets the project root namespace from the csproj file if defined, otherwise uses the project name.
102+ /// </summary>
103+ /// <returns>The root namespace for the project</returns>
104+ private string GetProjectRootNamespace ( )
105+ {
106+ try
107+ {
108+ if ( File . Exists ( CsProjectFile ) )
109+ {
110+ var xDocument = XDocument . Load ( CsProjectFile ) ;
111+ var rootNamespaceElement = xDocument . Descendants ( "RootNamespace" ) . FirstOrDefault ( ) ;
112+
113+ if ( rootNamespaceElement != null && ! string . IsNullOrWhiteSpace ( rootNamespaceElement . Value ) )
114+ {
115+ return rootNamespaceElement . Value ;
116+ }
117+ }
118+ }
119+ catch ( Exception ex )
120+ {
121+ // Log the error but continue with fallback
122+ Log . Logger . Warning ( $ "Failed to read RootNamespace from { CsProjectFile } : { ex . Message } ") ;
123+ }
124+
125+ // Fallback to csproj filename (without extension) if RootNamespace is not defined
126+ return MakeValidIdentifier ( Path . GetFileNameWithoutExtension ( CsProjectFile ) ) ;
127+ }
128+
100129
101130 private string EnsureOutputFolder ( )
102131 {
@@ -209,9 +238,9 @@ public static PlcTranslator Instance
209238
210239 private PlcTranslator()
211240 {{
212- var defaultResourceType = Assembly.GetAssembly(typeof({ this . ProjectRootNamespace } .PlcTranslator))
213- .GetType(""{ this . ProjectRootNamespace } .Resources.PlcStringResources"");
214- this.SetLocalizationResource(defaultResourceType );
241+ var assembly = Assembly.GetAssembly(typeof({ this . ProjectRootNamespace } .PlcTranslator));
242+ var resource = assembly .GetType(""{ this . ProjectRootNamespace } .Resources.PlcStringResources"");
243+ this.SetLocalizationResource(resource, assembly );
215244 }}
216245 }}
217246}}" ;
0 commit comments