C#: Use a namespace for a specific block of code?

后端 未结 8 1524
天涯浪人
天涯浪人 2021-01-17 12:21

I just have a point of curiosity. I\'m working with SSRS and calling its SOAP methods. I\'ve got stubs generated / Web references created and that\'s all working fine and I

相关标签:
8条回答
  • 2021-01-17 12:33

    While not relevant for working with Namespaces, or C#, VB.NET supports the With keyword which can be used as a shortcut for accessing members of an object:

    SomeReallyLongName.Property1 = 1
    SomeReallyLongName.Property2 = 2
    SomeReallyLongName.Property3 = 3
    SomeReallyLongName.Property4 = 4
    SomeReallyLongName.Property5 = 5
    

    Can be rewritten as:

    With SomeReallyLongName
        .Property1 = 1
        .Property2 = 2
        .Property3 = 3
        .Property4 = 4
        .Property5 = 5
    End With
    

    It's not in C#, as you can get very close to the same behavior using other approached:

    • Using shorter variable names
    • Using imports-aliasing (Such as Martin suggested)
    0 讨论(0)
  • 2021-01-17 12:35

    As others have written, I don't think this is possible. But what you can do, is to alias the full namespaces instead of each single class you want to use, e.g:

    using rs = ReportService2005;
    using re = ReportExecution;
    
    // ...
    
    rs.ParameterValue[] values = null;
    rs.DataSourceCredentials[] credentials = null;
    rs.ReportParameter[] parameters;
    re.ParameterValue v2 = ...;
    
    0 讨论(0)
  • 2021-01-17 12:39

    What you can do, is mark your class as partial:

    public partial class MyWebServiceClass
    

    in your main source file, and create a second source file with the method where you want to use the other namespace

    // MyWebServiceClass.usingMethods.cs
    
    using ReportService2005;
    public partial class MyWebServiceClass
    {
        // methods...
    }
    
    0 讨论(0)
  • 2021-01-17 12:49

    If the overlapping classes are identical (not just named the same) and share the same XML namespace, etc., then you may be able to take advantage of the wsdl.exe tool's sharetypes feature to generate both of your web service proxies so that they share the same type definitions for those overlapping classes.

    http://msdn.microsoft.com/en-us/library/7h3ystb6%28VS.80%29.aspx

    Check out the "/sharetypes" option to see if that works for your situation.

    0 讨论(0)
  • 2021-01-17 12:51

    I don't think you can do that. You must specify Fully Qualified name to do that.

    0 讨论(0)
  • 2021-01-17 12:52

    AFAIK it can't be done

    0 讨论(0)
提交回复
热议问题