I suspect that the short answer to this question is “no,” but I am interested in the ability to detect the use of a dynamic keyword at run time in C # 4.0, in particular, as a general parameter type for a method.
To give some background, we have the RestClient class in a library shared between several of our projects, which takes a type parameter to indicate the type that should be used when de-serializing the response, for example:
public IRestResponse<TResource> Get<TResource>(Uri uri, IDictionary<string, string> headers) where TResource : new() { var request = this.GetRequest(uri, headers); return request.GetResponse<TResource>(); }
Unfortunately (because of the reasons for which I will not go here for the sake of brevity) the use of dynamic, since the type parameter to return the dynamic type does not work properly - we had to add a second signature to the class to return the dynamic response type:
public IRestResponse<dynamic> Get(Uri uri, IDictionary<string, string> headers) { var request = this.GetRequest(uri, headers); return request.GetResponse(); }
However, using a dynamic type parameter for the first method raises a very strange error that masks the actual problem and debugs all this headache. To help other programmers using the API, I would like to try to determine whether dynamic is used in the first method, so that it either does not compile at all, or when an exception is used, then saying something along the lines "use this other method if you want to get a dynamic type response. "
Mostly:
public IRestResponse<TResource> Get<TResource>(Uri uri, IDictionary<string, string> headers) where TResource is not dynamic
or
public IRestResponse<TResource> Get<TResource>(Uri uri, IDictionary<string, string> headers) where TResource : new() { if (typeof(TResource).isDynamic()) { throw new Exception(); } var request = this.GetRequest(uri, headers); return request.GetResponse<TResource>(); }
Is there one of these things? We use VS2010 and .Net 4.0, but I will be interested in the solution .Net 4.5 for further use, if it is possible to use new language functions.