Why can't I make a public partial class "extension" on a service generated class?

I have a WCF service I’m using from Silverlight code. I wanted to “extend” the public partial class that was generated, but the compiler doesn’t allow me to.

The slSvcUtil service generated code has essentially this:


namespace;

interface

uses
    System.Runtime.Serialization;

...

[System.Diagnostics.DebuggerStepThroughAttribute]
[System.CodeDom.Compiler.GeneratedCodeAttribute('System.Runtime.Serialization', '4.0.0.0')]
[System.Runtime.Serialization.DataContractAttribute(Name := 'Settings', &Namespace := 'http://schemas.datacontract.org/2004/07/Guardian.Settings.DataContracts')]
Guardian.Settings.DataContracts.Settings = public partial class(System.Object)
private 
    var TerminologyField: System.Collections.Generic.Dictionary;
    method get_Terminology: System.Collections.Generic.Dictionary;
    method set_Terminology(value: System.Collections.Generic.Dictionary);
public
    [System.Runtime.Serialization.DataMemberAttribute]
    property Terminology: System.Collections.Generic.Dictionary read get_Terminology write set_Terminology;
end;
...

This code is part of a separate project assembly with no other code and no other project references.

Then I added this class file:

namespace Guardian.Settings.DataContracts;

interface

   type
      Settings = public partial class( System.Object )
         public
            property AtRiskLabel             : String read GetAtRiskLabel write fAtRiskLabel; notify;
         private
            fAtRiskLabel: String;
            method GetAtRiskLabel: String;
         end;

implementation

   method Settings.GetAtRiskLabel: String;
      begin
      if fAtRiskLabel = nil then begin
         fAtRiskLabel := Terminology[ 'AtRisk' ];
         end;

      result := fAtRiskLabel;
      end;

end.

And I get a compiler error that says it doesn’t know the identifier Terminology.

If I add this file to the project:

namespace Guardian.Settings.DataContracts;

interface

implementation

end.

Then it compiles fine.

I have had this problem before and forgot my past solution of adding that bogus namespace file. I don’t think I posted about this before, but thought I should post it now.

So is this a bug, or should I have done something different to get the partial class to compile properly?

I’m not sure if i fully understand what you are trying to do, but: partial classes need to have all parts defined in the same assembly.

Yeah, I had already bumped into the same assembly requirement. But both of the files I listed above are in the same project. And they are the only files in the project.

When I said a “separate assembly”, I meant that both files were in the same, separate assembly from anything else in the solution.

And I added the “bogus” namespace file to get it to compile, so there are now 3 files in the project. The two for the partial classes and the one to help resolve the namespace from the service generated file.

I think the problem stems from the fact that the namespace in the generated file is just “namespace;”. No named namespace. So I think the file that wants to reference the classes in there doesn’t know about the namespace “Guardian.Settings.DataContracts” until I add in the extra file (even when that file has nothing defined in it.

This does sound like a bug then, possibly caused by the different namespace syntaxes (per-unit vs on-the-class-decl). can you send us the exact project that shows this?

Here is the assembly. It includes the Bogus file that resolves the namespace issue. If that file is removed from the project, then the compiler fails.

bugs://64483: Problem with partial class if one part has the namespace locally on the class decl, thanx

@mtiede: can you try this with the ‘alpha’ builds? I cannot reproduce this issue

My version is getting old already and I don’t want to goof up what I have as I’m in the middle of some serious modifications to a major project.

When I get done with that, hopefully I can update my version and I can check then. If you can’t reproduce it, that sounds like the problem has been fixed.

Thanks.