Method input parameter as namespace

Let's say I have these objects

namespace MyResponses.Interfaces.IInterface1
{
    partial class exResponse
    {
        Object1 myObj; 
        bool flag;
    }
}

namespace MyResponses.Interfaces.IInterface2
{
    partial class exResponse
    {
        Object2 myObj;
        bool flag;
    }
}

namespace MyResponses.Interfaces.IInterface3
{
    partial class exResponse
    {
        Object3 myObj; 
        bool flag;
    }
}

And I need a method to check the flags in all exResponse objects. This method would receive an object inside MyResponses.Interfaces namespace.

Something like:

bool CheckFlagInResponse(MyResponses.Interfaces response, Type responseType)
{
    return (response as responseType).flag;
}

I would call it like this?

CheckFlagInResponse(myExResponse, myExResponse.GetType())
Jon Skeet
people
quotationmark

It sounds like this is basically broken by design, if you have very similar types all with the same members (and even the same simple name) but with no type relationship between them.

If this is generated code and the types are all declared as partial types in the generated code, you could potentially fix this by making them all implement an interface:

public interface IFlag
{
    bool Flag { get; }
}

namespace MyResponses.Interfaces.IInterface1
{
    public partial class ExResponse : IFlag {}
}

namespace MyResponses.Interfaces.IInterface2
{
    public partial class ExResponse : IFlag {}
}

// etc

Then you can just use IFlag for anything that needs to access the flag - all the response classes would implement the interface.

Alternatively, if you really have to (and if you're using .NET 4, which it now sounds like you're not), you could just use dynamic typing:

bool CheckFlagInResponse(dynamic response)
{
    return response.flag;
}

people

See more on this question at Stackoverflow