4

Today when I tried to port some existing code from .net to Xamarin (Mono for Android) I encountered the problem that [MethodImpl(MethodImplOptions.AggressiveInlining)] did not compile for Android because the MethodImplOptions doesn't have an AggressiveInlining value when compiling for Android.

I want to know if it is possible to get this code to compile without having to change all lines of code?

I already tried to create an attribute with the same name but ran into compile errors too because 2 enums had the same name (ambigious).

What I would like is some way to inject the AggressiveInlining value in the existing Enum and just give it a value of 0 or something similar so nothing happens.

I hope someone can help me with this problem.

Kind regards,

Devedse

Devedse
  • 1,801
  • 1
  • 19
  • 33
  • You can't update your code to pass a different value if it's Xamarin/Android, say using [compiling directives](http://msdn.microsoft.com/en-us/library/4y6tbswk.aspx)? EDIT: Or simply remove the attribute altogether. – Chris Sinclair Jun 11 '13 at 18:54
  • 1
    The problem with compiling directives is that I would have to add them on every place this is currently being used, if possible I would like to avoid that. – Devedse Jun 11 '13 at 18:56

1 Answers1

2

AFAIK, you can't add values to enumerations; the type is already compiled into the library. (you can set them to other values by casting the underlying type value to the enum value, but I don't think that's applicable in your case with the attribute)

Since you have many places where you're using it that you'd have to update with directives, I would suggest moving the desired MethodImplOption value to a centrally accessible constant and have everything reference that:

public class MethodImplSetting
{
    public const MethodImplOptions DEFAULT_METHOD_IMPL = MethodImplOptions.AggressiveInlining;
}

[MethodImpl(DEFAULT_METHOD_IMPL)]
public void MyMethod()
{

}

Then you can apply compiler directives to effortlessly switch it to your special Android value when compiling:

public class MethodImplSetting
{
    public const MethodImplOptions DEFAULT_METHOD_IMPL =
    #if  __ANDROID__
        MethodImplOptions.NoInlining; //or whatever value you want
    #else
        MethodImplOptions.AggressiveInlining;
    #endif
}

As you support additional platforms or want to switch different method implementation options for testing, it becomes trivial now to do so.

I believe the appropriate Xamarin Android directive is __ANDROID__ from their documentation at Platform Divergence/Abstraction/Divergent Implementation, section 3.1.2 but you may want to double-check that.

Chris Sinclair
  • 22,858
  • 3
  • 52
  • 93