New Linq To Xsd Proect won't compile.

Feb 27, 2012 at 4:17 PM

Hi,

We use Linq To XSD a lot, so this really has me stumped.

New project, added LinqToXsd to the csproj file, added the schema, set it to LinqToXsd and compiled.

Project won't compile, lots of errors in LinqToXsdSource.cs.

It seems it can't see any of the System namespaces, lots of:

Error    1    A using namespace directive can only be applied to namespaces; 'System' is a type not a namespace \obj\Debug\LinqToXsdSource.cs

I've diff'd all the csproj files and linqtoxsd binaries, no difference (apart from they are different projects)

I'm stumped here, anyone?

J.

 

Feb 27, 2012 at 4:33 PM

Oh dear, I've just figured out what the problem is.

It's not that is can't see System, it's that my schema has an item in it called System, and that's why it can't see System, it's masked by this new type.

Any solutions to this beyond changing the schema?

Feb 28, 2012 at 12:01 PM

Fixed it, solution was: (this is in the 1.1 source code)

find this: "System.

replace it with this: "global::System.

In XObjects/Src/ClrTypeInfo.cs:

replace: clrTypeName = st.GetListItemType().Datatype.ValueType.ToString();

with: clrTypeName = "global::" + st.GetListItemType().Datatype.ValueType.ToString();

and replace: clrTypeName = st.Datatype.ValueType.ToString();

with: clrTypeName = "global::" + st.Datatype.ValueType.ToString();

For what it's worth, here's the diff file our svn server creates:

Index: XObjects/Src/ClrPropertyInfo.cs
===================================================================
--- XObjects/Src/ClrPropertyInfo.cs	(revision 951)
+++ XObjects/Src/ClrPropertyInfo.cs	(working copy)
@@ -595,7 +595,7 @@
                         returnType = CreateListReturnType(fullTypeName);
                     }
                     else if (!IsRef && typeRef.IsValueType && IsNullable ) {
-                        returnType = new XCodeTypeReference("System.Nullable", new CodeTypeReference(fullTypeName));
+                        returnType = new XCodeTypeReference("global::System.Nullable", new CodeTypeReference(fullTypeName));
                     }
                     else {
                         returnType = new XCodeTypeReference(clrTypeName);
Index: XObjects/Src/ClrTypeInfo.cs
===================================================================
--- XObjects/Src/ClrTypeInfo.cs	(revision 951)
+++ XObjects/Src/ClrTypeInfo.cs	(working copy)
@@ -580,9 +580,9 @@
                 XmlSchemaSimpleType st = schemaObject as XmlSchemaSimpleType;
                 Debug.Assert(st != null);
                 if (IsSchemaList)
-                    clrTypeName = st.GetListItemType().Datatype.ValueType.ToString();
+                    clrTypeName = "global::" + st.GetListItemType().Datatype.ValueType.ToString();
                 else
-                    clrTypeName = st.Datatype.ValueType.ToString();
+                    clrTypeName = "global::" + st.Datatype.ValueType.ToString();
             }
             return clrTypeName;
         }
Index: XObjects/Src/Constants.cs
===================================================================
--- XObjects/Src/Constants.cs	(revision 951)
+++ XObjects/Src/Constants.cs	(working copy)
@@ -102,7 +102,7 @@
         public const string GetFSM = "GetValidationStates";
         public const string InitFSM = "InitFSM";
         public const string TransitionsVar = "transitions";
-        public const string Int = "System.Int32";
+        public const string Int = "global::System.Int32";
         public const string SingleTrans = "SingleTransition";
         public const string WildCard = "WildCard";
 
Index: XObjects/Src/SimpleTypeCodeDomHelper.cs
===================================================================
--- XObjects/Src/SimpleTypeCodeDomHelper.cs	(revision 951)
+++ XObjects/Src/SimpleTypeCodeDomHelper.cs	(working copy)
@@ -105,7 +105,7 @@
             if ((flags & RestrictionFlags.Enumeration) != 0)
             {
                 CodeArrayCreateExpression enums = new CodeArrayCreateExpression();
-                enums.CreateType = new CodeTypeReference("System.Object");
+                enums.CreateType = new CodeTypeReference("global::System.Object");
                 
                 foreach (object o in facets.Enumeration)
                 {
Index: XObjects/Src/TypeBuilder.cs
===================================================================
--- XObjects/Src/TypeBuilder.cs	(revision 951)
+++ XObjects/Src/TypeBuilder.cs	(working copy)
@@ -146,19 +146,19 @@
             bool useAutoTyping = clrTypeInfo.IsAbstract || clrTypeInfo.IsSubstitutionHead;
             if (clrTypeInfo.typeOrigin == SchemaOrigin.Element) { //Disable load and parse for complex types
                 CodeTypeMember load = CodeDomHelper.CreateStaticMethod(
-                    "Load", clrTypeName, innerType, "xmlFile", "System.String", useAutoTyping);
+                    "Load", clrTypeName, innerType, "xmlFile", "global::System.String", useAutoTyping);
                 // http://linqtoxsd.codeplex.com/WorkItem/View.aspx?WorkItemId=4093
                 var loadReader = CodeDomHelper.CreateStaticMethod(
-                    "Load", clrTypeName, innerType, "xmlFile", "System.IO.TextReader", useAutoTyping);
-                CodeTypeMember parse = CodeDomHelper.CreateStaticMethod("Parse", clrTypeName, innerType, "xml", "System.String", useAutoTyping);
+                    "Load", clrTypeName, innerType, "xmlFile", "global::System.IO.TextReader", useAutoTyping);
+                CodeTypeMember parse = CodeDomHelper.CreateStaticMethod("Parse", clrTypeName, innerType, "xml", "global::System.String", useAutoTyping);
                 if (clrTypeInfo.IsDerived) {
                     load.Attributes |= MemberAttributes.New;
                     parse.Attributes |= MemberAttributes.New;
                 }
                 else {
-                    decl.Members.Add(CodeDomHelper.CreateSave("xmlFile", "System.String"));
-                    decl.Members.Add(CodeDomHelper.CreateSave("tw", "System.IO.TextWriter"));
-                    decl.Members.Add(CodeDomHelper.CreateSave("xmlWriter", "System.Xml.XmlWriter"));
+                    decl.Members.Add(CodeDomHelper.CreateSave("xmlFile", "global::System.String"));
+                    decl.Members.Add(CodeDomHelper.CreateSave("tw", "global::System.IO.TextWriter"));
+                    decl.Members.Add(CodeDomHelper.CreateSave("xmlWriter", "global::System.Xml.XmlWriter"));
                 }                
                 decl.Members.Add(load);
                 decl.Members.Add(loadReader);
@@ -391,16 +391,16 @@
             //Create static constructor
             CodeTypeConstructor staticServicesConstructor = new CodeTypeConstructor();
 
-            CodeTypeReference returnType = CodeDomHelper.CreateDictionaryType("XName", "System.Type");
-            CodeTypeReference wrapperReturnType = CodeDomHelper.CreateDictionaryType("System.Type", "System.Type");
+            CodeTypeReference returnType = CodeDomHelper.CreateDictionaryType("XName", "global::System.Type");
+            CodeTypeReference wrapperReturnType = CodeDomHelper.CreateDictionaryType("global::System.Type", "global::System.Type");
             
             //Create a dictionary of TypeName vs System.Type and the method to create it
             CodeMemberProperty typeDictProperty = null;
             if (typeDictionaryStatements.Count > 0) {
                 
                 typeDictProperty = CodeDomHelper.CreateInterfaceImplProperty(Constants.GlobalTypeDictionary, Constants.ILinqToXsdTypeManager, returnType, Constants.TypeDictionaryField);
-                
-                CodeMemberField staticTypeDictionary = CodeDomHelper.CreateDictionaryField(Constants.TypeDictionaryField, "XName", "System.Type");
+
+                CodeMemberField staticTypeDictionary = CodeDomHelper.CreateDictionaryField(Constants.TypeDictionaryField, "XName", "global::System.Type");
                 CodeMemberMethod buildTypeDictionary = CodeDomHelper.CreateMethod(Constants.BuildTypeDictionary, privateStatic, null);
                 buildTypeDictionary.Statements.AddRange(typeDictionaryStatements);
 
@@ -419,8 +419,8 @@
             CodeMemberProperty elementDictProperty = null;
             if (elementDictionaryStatements.Count > 0) {
                 elementDictProperty = CodeDomHelper.CreateInterfaceImplProperty(Constants.GlobalElementDictionary, Constants.ILinqToXsdTypeManager, returnType, Constants.ElementDictionaryField);
-                
-                CodeMemberField staticElementDictionary = CodeDomHelper.CreateDictionaryField(Constants.ElementDictionaryField, "XName", "System.Type");
+
+                CodeMemberField staticElementDictionary = CodeDomHelper.CreateDictionaryField(Constants.ElementDictionaryField, "XName", "global::System.Type");
                 CodeMemberMethod buildElementDictionary = CodeDomHelper.CreateMethod(Constants.BuildElementDictionary, privateStatic, null);
                 buildElementDictionary.Statements.AddRange(elementDictionaryStatements);
 
@@ -439,8 +439,8 @@
             CodeMemberProperty wrapperDictProperty = null;
             if (wrapperDictionaryStatements.Count > 0) {
                 wrapperDictProperty = CodeDomHelper.CreateInterfaceImplProperty(Constants.RootContentTypeMapping, Constants.ILinqToXsdTypeManager, wrapperReturnType, Constants.WrapperDictionaryField);
-                
-                CodeMemberField staticWrapperDictionary = CodeDomHelper.CreateDictionaryField(Constants.WrapperDictionaryField, "System.Type", "System.Type");
+
+                CodeMemberField staticWrapperDictionary = CodeDomHelper.CreateDictionaryField(Constants.WrapperDictionaryField, "global::System.Type", "global::System.Type");
                 CodeMemberMethod buildWrapperDictionary = CodeDomHelper.CreateMethod(Constants.BuildWrapperDictionary, privateStatic, null);
                 buildWrapperDictionary.Statements.AddRange(wrapperDictionaryStatements);
                 
@@ -470,7 +470,7 @@
                 addSchemasMethod.Statements.Add(CodeDomHelper.CreateMethodCall(new CodeVariableReferenceExpression("schemas"), "Add", new CodeFieldReferenceExpression(null, schemaSetFieldName)));
 
                 
-                CodeTypeReferenceExpression interLockedType = new CodeTypeReferenceExpression("System.Threading.Interlocked");
+                CodeTypeReferenceExpression interLockedType = new CodeTypeReferenceExpression("global::System.Threading.Interlocked");
 
                 CodeMemberProperty schemaSetProperty = CodeDomHelper.CreateInterfaceImplProperty("Schemas", Constants.ILinqToXsdTypeManager, schemaSetType);
                 CodeFieldReferenceExpression schemaSetFieldRef = new CodeFieldReferenceExpression(null, schemaSetFieldName);
@@ -511,7 +511,7 @@
             CodeMemberMethod getRootType = new CodeMemberMethod(); 
             getRootType.Attributes = MemberAttributes.Static | MemberAttributes.Public;
             getRootType.Name = Constants.GetRootType;
-            getRootType.ReturnType = new CodeTypeReference("System.Type");
+            getRootType.ReturnType = new CodeTypeReference("global::System.Type");
             if (rootElementName.IsEmpty) {
                 getRootType.Statements.Add(
                     new CodeMethodReturnStatement(
@@ -730,10 +730,10 @@
         }
 
         private CodeMemberProperty BuildLocalElementDictionary() {
-            CodeMemberProperty localDictionaryProperty = CodeDomHelper.CreateInterfaceImplProperty(Constants.LocalElementsDictionary, Constants.IXMetaData, CodeDomHelper.CreateDictionaryType(Constants.XNameType, "System.Type"));
+            CodeMemberProperty localDictionaryProperty = CodeDomHelper.CreateInterfaceImplProperty(Constants.LocalElementsDictionary, Constants.IXMetaData, CodeDomHelper.CreateDictionaryType(Constants.XNameType, "global::System.Type"));
 
             //new override for derived classes
-            CodeMemberField localDictionaryField = CodeDomHelper.CreateDictionaryField(Constants.LocalElementDictionaryField, "XName", "System.Type");
+            CodeMemberField localDictionaryField = CodeDomHelper.CreateDictionaryField(Constants.LocalElementDictionaryField, "XName", "global::System.Type");
             CodeMemberMethod localDictionaryMethod = CodeDomHelper.CreateMethod(Constants.BuildElementDictionary, MemberAttributes.Private | MemberAttributes.Static, null);
             localDictionaryMethod.Statements.AddRange(propertyDictionaryAddStatements);
 
@@ -850,7 +850,7 @@
             CodeConstructor dummyConstructor = null; 
             if (clrTypeInfo.IsSubstitutionHead) { //Add dummy constructor that derived classes can call
                 dummyConstructor = CodeDomHelper.CreateConstructor(MemberAttributes.Family);
-                dummyConstructor.Parameters.Add(new CodeParameterDeclarationExpression("System.Boolean", "setNull"));
+                dummyConstructor.Parameters.Add(new CodeParameterDeclarationExpression("global::System.Boolean", "setNull"));
                 decl.Members.Add(dummyConstructor);
             }
             
@@ -897,7 +897,7 @@
         }
 
         protected override void ImplementCommonIXMetaData() {
-            CodeMemberProperty localElementDictionary = CodeDomHelper.CreateInterfaceImplProperty(Constants.LocalElementsDictionary, Constants.IXMetaData, CodeDomHelper.CreateDictionaryType(Constants.XNameType, "System.Type"));
+            CodeMemberProperty localElementDictionary = CodeDomHelper.CreateInterfaceImplProperty(Constants.LocalElementsDictionary, Constants.IXMetaData, CodeDomHelper.CreateDictionaryType(Constants.XNameType, "global::System.Type"));
             localElementDictionary.GetStatements.Add(CodeDomHelper.CreateCastToInterface(Constants.IXMetaData, "schemaMetaData", Constants.CInnerTypePropertyName));
             localElementDictionary.GetStatements.Add(
                     new CodeMethodReturnStatement(
Index: XObjects/Src/TypesToCodeDom.cs
===================================================================
--- XObjects/Src/TypesToCodeDom.cs	(revision 951)
+++ XObjects/Src/TypesToCodeDom.cs	(working copy)
@@ -259,9 +259,9 @@
             xroot.Members.Add(CodeDomHelper.CreateConstructor(MemberAttributes.Private));            
 
             //Load Methods
-            xroot.Members.Add(CodeDomHelper.CreateXRootMethod(rootName, "Load", new string[][]{new string[]{"System.String",  "xmlFile"}}));
+            xroot.Members.Add(CodeDomHelper.CreateXRootMethod(rootName, "Load", new string[][]{new string[]{"global::System.String",  "xmlFile"}}));
 
-            xroot.Members.Add(CodeDomHelper.CreateXRootMethod(rootName, "Load", new string[][]{new string[]{"System.String",  "xmlFile"}, 
+            xroot.Members.Add(CodeDomHelper.CreateXRootMethod(rootName, "Load", new string[][]{new string[]{"global::System.String",  "xmlFile"}, 
                                                                                              new string[]{"LoadOptions",  "options"}}));
             
 
@@ -275,20 +275,20 @@
 
 
             //Parse Methods
-            xroot.Members.Add(CodeDomHelper.CreateXRootMethod(rootName, "Parse", new string[][]{new string[]{"System.String",  "text"}}));
+            xroot.Members.Add(CodeDomHelper.CreateXRootMethod(rootName, "Parse", new string[][]{new string[]{"global::System.String",  "text"}}));
 
-            xroot.Members.Add(CodeDomHelper.CreateXRootMethod(rootName, "Parse", new string[][]{new string[]{"System.String",  "text"}, 
+            xroot.Members.Add(CodeDomHelper.CreateXRootMethod(rootName, "Parse", new string[][]{new string[]{"global::System.String",  "text"}, 
                                                                                               new string[]{"LoadOptions",  "options"}}));
 
 
             //Save Methods
-            xroot.Members.Add(CodeDomHelper.CreateXRootSave(new string[][]{new string[]{"System.String",  "fileName"}}));
+            xroot.Members.Add(CodeDomHelper.CreateXRootSave(new string[][]{new string[]{"global::System.String",  "fileName"}}));
             xroot.Members.Add(CodeDomHelper.CreateXRootSave(new string[][]{new string[]{"TextWriter",  "textWriter"}}));
             xroot.Members.Add(CodeDomHelper.CreateXRootSave(new string[][]{new string[]{"XmlWriter",  "writer"}}));
 
             xroot.Members.Add(CodeDomHelper.CreateXRootSave(new string[][]{new string[]{"TextWriter",  "textWriter"}, 
                                                                            new string[]{"SaveOptions",  "options"}}));
-            xroot.Members.Add(CodeDomHelper.CreateXRootSave(new string[][]{new string[]{"System.String",  "fileName"}, 
+            xroot.Members.Add(CodeDomHelper.CreateXRootSave(new string[][]{new string[]{"global::System.String",  "fileName"}, 
                                                                            new string[]{"SaveOptions",  "options"}}));
 
             CodeMemberProperty prop = CodeDomHelper.CreateProperty("XDocument", 
@@ -432,18 +432,18 @@
         }
 
         private void AddDefaultImports(CodeNamespace newCodeNamespace) {
-            newCodeNamespace.Imports.Add(new CodeNamespaceImport("System"));
-            newCodeNamespace.Imports.Add(new CodeNamespaceImport("System.Collections"));
-            newCodeNamespace.Imports.Add(new CodeNamespaceImport("System.Collections.Generic"));
-            newCodeNamespace.Imports.Add(new CodeNamespaceImport("System.IO"));
-            newCodeNamespace.Imports.Add(new CodeNamespaceImport("System.Diagnostics"));
-            newCodeNamespace.Imports.Add(new CodeNamespaceImport("System.Xml"));
-            newCodeNamespace.Imports.Add(new CodeNamespaceImport("System.Xml.Schema"));
+            newCodeNamespace.Imports.Add(new CodeNamespaceImport("global::System"));
+            newCodeNamespace.Imports.Add(new CodeNamespaceImport("global::System.Collections"));
+            newCodeNamespace.Imports.Add(new CodeNamespaceImport("global::System.Collections.Generic"));
+            newCodeNamespace.Imports.Add(new CodeNamespaceImport("global::System.IO"));
+            newCodeNamespace.Imports.Add(new CodeNamespaceImport("global::System.Diagnostics"));
+            newCodeNamespace.Imports.Add(new CodeNamespaceImport("global::System.Xml"));
+            newCodeNamespace.Imports.Add(new CodeNamespaceImport("global::System.Xml.Schema"));
             if (settings.EnableServiceReference) {
-                newCodeNamespace.Imports.Add(new CodeNamespaceImport("System.Xml.Serialization"));
+                newCodeNamespace.Imports.Add(new CodeNamespaceImport("global::System.Xml.Serialization"));
             }
-            newCodeNamespace.Imports.Add(new CodeNamespaceImport("System.Xml.Linq"));
-            newCodeNamespace.Imports.Add(new CodeNamespaceImport("Xml.Schema.Linq"));
+            newCodeNamespace.Imports.Add(new CodeNamespaceImport("global::System.Xml.Linq"));
+            newCodeNamespace.Imports.Add(new CodeNamespaceImport("global::Xml.Schema.Linq"));
         }
 
         private TypeBuilder GetTypeBuilder() {