Indicate a member of the interface not by name, but enter

I have many similar classes generated by svcutil from some external WSDL file. Any class has a property Headerand a property stringcalled class name + "1".

For example, I have classes:, SimpleRequestwhich has a property Headerand a property SimpleRequest1.
The other is ComplexRequest, with the property Headerand ComplexRequest1.

So, I want to create a common interface for such classes. So basically I can define something like this:

interface ISomeRequestClass {
  string Header;
  // here is some definition for `class name + "1"` properties...
}

Is it possible to define such an element in the interface?


Here is the message being edited ...

Here is an example of a generated class:

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
[System.ServiceModel.MessageContractAttribute(IsWrapped=false)]
public partial class SimpleRequest
{


    public string Header;

    [System.ServiceModel.MessageBodyMemberAttribute(Name="SimpleRequest", Namespace="data", Order=0)]
    public SimpleRequestMsg SimpleRequest1;

    public SimpleRequest()
    {
    }

    public SimpleRequest(string Header, SimpleRequestMsg SimpleRequest1)
    {
        this.Header = Header;
        this.SimpleRequest1 = SimpleRequest1;
    }
}

WORK REPORT 2

+1, . . , ?


3

, .

+3
5

( ): , Header, Header. , . , , , .


?

, . , . ISomeRequestClass, ?

:

interface ISomeRequestClass {
    string Header { get; set; }
    string ClassName1 { get; set; }
}

class SomeClass : ISomeRequestClass {
     string Header { ... }

     string SomeClass1 { ... }

     // new: explicit interface implementation
     string ISomeRequestClass.ClassName1 {
          get { return SomeClass1; }
          set { SomeClass1 = value; }
     }
}
+3

:

interface ISomeRequestClass {
  string HeaderProp {get; set;}
  string Prop {get; set;}
}

( ) :

public partial class SimpleRequest : ISomeRequestClass
{
  public string HeaderProp
  {
    get
    {
      return Header;
    }
    set
    {
      Header = value;
    }
  }

  public string Prop
  {
    get
    {
      return SimpleRequest1;
    }
    set
    {
      SimpleRequest1= value;
    }
  }
}
+3

.

, +1, .

<+ strong >

+1 , :

public interface IFoo
{
 [...]
 PlusOneBaseType MyPlusOneObject{get;set;}
}

+1 :

public interface IFoo<TPlusOneType>
{
 [...]
 TPlusOneType MyPlusOneObject{get;set;}
}

, :

public class SimpleRequest : IFoo<SimpleRequest1>
{
 [...]
}

, , () , .

+2

svcutil, , WCF DataContracts?

, name DataMemberAttribute.

interface IRequest 
{
    string Header { get; set; }
    string Request1 { get; set; }
}

[DataContract]
class SimpleRequest : IRequest
{
    [DataMember]
    public string Header { get; set; }

    [DataMember(Name="SimpleRequest1"]
    public string Request1 { get; set; }
}

[DataContract]
class ComplexRequest : IRequest
{
    [DataMember]
    public string Header { get; set; }

    [DataMember(Name="ComplexRequest1"]
    public string Request1 { get; set; }
}

, , - , PowerShell script, . svcutil - script, - Microsoft. "" "". script scvutil, .

 

EDIT ( )

MessageBodyMemberAttribute name, :

public string SimpleRequest1; 

To

public string Request1; 
+2

Do you really need these classes to have a common interface? I will be tempted to create a shell interface (or just a specific class) instead, which can then use reflection to access the corresponding fields:

// TODO: Make this class implement an appropriate new interface if you want
// to, for mocking purposes.
public sealed class RequestWrapper<TRequest, TMessage>
{
    private static readonly FieldInfo headerField;
    private static readonly FieldInfo messageField;

    static RequestWrapper()
    {
        // TODO: Validation
        headerField = typeof(TRequest).GetField("Header");
        messageField = typeof(TRequest).GetField(typeof(TRequest).Name + "1");
    }

    private readonly TRequest;

    public RequestWrapper(TRequest request)
    {
        this.request = request;
    }

    public string Header
    {
        get { return (string) headerField.GetValue(request); }
        set { headerField.SetValue(request, value); }
    }

    public TMessage Message
    {
        get { return (TMessage) messageField.GetValue(request); }
        get { messageField.SetValue(request, value); }
    }
}

You can use expression trees to create delegates for this if reflection is too slow, but I would stick with a simple solution to get you started.

The advantage of this is that you only need to write this code once, but that means creating a wrapper around real query objects that are not in the responses of the partial class.

0
source

All Articles