-
Notifications
You must be signed in to change notification settings - Fork 6
Handling missing instance properties
Handling missing instance methods in the PCL version of a specific type is fairly straightforward. Using extension methods it is possible to exactly mimic the signature of the original method.
For example, Stream.Close()
can be substituted with StreamExtensions.Close(this Stream)
. In source code, the extension method can be expressed with the same signature as the instance method is intended to mimic.
For instance properties missing in PCL, this is a slightly different matter. C# does not support extension properties, so it is not possible to completely mimic the signature of an instance property.
The workaround for this issue in Shim is to implement extension methods with the same name as the instance property it is intended to substitute.
For example, for the Windows 8 platform the Type.IsEnum
instance property has been deprecated in favor of TypeInfo.IsEnum
. Now, to minimize the modification requirements in the legacy code, Shim implements the extension method TypeExtensions.IsEnum(this Type)
to substitute Type.IsEnum
.
In the legacy code, instead of inserting a .GetTypeInfo().
call, it will only be necessary to make the following minor modification:
Type someType;
var isEnum = someType.IsEnum; ---> var isEnum = someType.IsEnum();
(Shim version 2) For instance property setters, extension methods are similarly used. For example, to substitute
class WebRequest {
int Timeout { set; }
}
Shim implements
static class WebRequestExtensions {
static void Timeout(this WebRequest, int);
}
Which in legacy code translates to the following modification:
WebRequest request;
request.Timeout = 100; ---> request.Timeout(100);