質問

Im sorry for if the question is bit unclear. please feel free to clearup/generelize this question. I don't know english well enough to do it myself

I need to use a web service all the methods of the web service take as first parameter an authentication object

class webservice{
    e_webservice_return_value method1(object authentication,object param1,object param2,object param32)
    e_webservice_return_value method2(object authentication,object param1,object param2)
    .....
}

there are hundreds of methods.

I want to create a provider class that will provide all methodes now I can create a class named provider and write for each method

class provider{
    object _auth;
    e_webservice_return_value method1(object param1,object param2,object param3)
    {
        var res= method1(_auth,param1,param2,param3);
        if(res=bad auth)
        {
            ReAuth(auth);
        }
        return res;
    }

    e_webservice_return_value method2(object param1,object param2)
    {
        var res =return e_webservice_return_value method2(_auth,param1,param2);
        if(res=bad auth)
        {
            ReAuth(auth);
        }
        return res;
    }
}

I need this pattern for all the hundreds of methods.

is there a technique to create such a facade object without actually writing 100ds of functions all with same functionality? (only first parameter is the same) others may differ

役に立ちましたか?

解決

You might want to go with Reflection.Emit. A nice, but maybe a bit outdated tutorial, can be found at this site. Also take a look at MSDN - they have great documentation!

What you generally would like to do is to mark each method (or a whole class) with an attribute and for each such a method/class with an attribute create a wrapping method, and replace the code. You could make it run on application initialization or on the first call to this class/method, the design is up to you.

This is no easy task, but actually we had the same case in our project, and it proved to be quite a good solution. That's because each method with a given attribute required a similar handling.

Keep in mind that there are some alternative approaches to code emitting, but I do not know them well, so I cannot provide any suggestions - but it'd be easily googlable.

Reflection.Emit with IL by itself is quite troublesome, but certainly it allows you to learn a deal more about what's going on in your c# code, under the hood.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top