In .NET IL, is it acceptable to implement interface properties and events directly as methods?

In my reflexive C # code, I iterate over the methods on an interface and emit a class that: a) is declared to implement the interface; b) has all methods implemented with return GetMethods()

.

var methods = typeof(T).GetMethods(); // T is interface
foreach (var methodInfo in methods)
{
    var parameters = methodInfo.GetParameters().Select(p => p.ParameterType).ToArray();
    var method = typeBuilder.DefineMethod(
        methodInfo.Name,
        MethodAttributes.Public | MethodAttributes.Virtual,
        methodInfo.ReturnType,
        parameters);
        ... // Emit IL

      

This creates not only methods, but also properties and events as method pairs (get_ set_ / add_ remove _).

A dynamically created class is accepted by the CLR as an implementation of the interface and raises properties and events on the object (distinguished as an interface), works fine. However, the type builder has DefineProperty

as well DefineMethod

. Using ildasm, I can confirm that ".property" is missing from the declaration by just using DefineMethod

. Is it naughty to implement interface properties and events as if they were "fair" methods? Or is it completely legal?

+3


source to share


1 answer


Is it wrong to implement interface properties and events as if they were "fair" methods?



Yes it is. You won't be able to use properties as properties (when used dynamic

).

+4


source







All Articles