jeudi 7 mai 2015

Differentiating Between OutAttribute and out Modifier in Reflection

If I write this:

public interface IOutModifier
{
    void OutModifier(out int a);
}

And try to implement it in an interface, VS generates this (as expected):

public class IOM : IOutModifier
{
    public void OutModifier(out int a)
    {
        throw new NotImplementedException();
    }
}

If I write this:

public interface IOutAndOutAttributeModifier
{
    void OutAndOutAttributeModifier([Out] out int a);
}

VS will implement it like this:

public class IOOAM : IOutAndOutAttributeModifier
{
    public void OutAndOutAttributeModifier([Out]out int a)
    {
        throw new NotImplementedException();
    }
}

Side note: writing this:

public interface IOutAttributeModifier
{
    void OutAttributeModifier([Out] int a);
}

will be implemented like this:

public class IOAM : IOutAttributeModifier
{
    public void OutAttributeModifier([Out] int a)
    {
        throw new NotImplementedException();
    }
}

So, there seems to be a way to differentiate between the OutAttribute being present or not...but I can't figure out how (via Reflection). In both cases any of the methods to get custom attribute information (GetCustomAttributes(), GetCustomAttributeData(), etc.) report that the OutAttribute exists on all interface methods. This isn't a case either with code existing in the current project - if I reference an assembly with these interfaces, VS still generates the same code shown above.

So, how can I tell the difference between a parameter that is just "out" and one that had the "[Out]" attribute added to it explicitly?





Aucun commentaire:

Enregistrer un commentaire