Why do I get ambiguous error
-
21-12-2019 - |
Question
In Windows 8 it worked fine. When I upgraded it to Windows 8.1 I got an error:
Error 1 The call is ambiguous between the following methods or properties: 'System.IO.WindowsRuntimeStreamExtensions.AsRandomAccessStream(System.IO.Stream)' and 'EventHandler.UI.Extensions.StreamExtensions.AsRandomAccessStream(System.IO.Stream)'...
Here is my Method that gets the error:
public BitmapImage ConvertByteArrayToBitMapImage(byte[] imageByteArray)
{
BitmapImage bitmapImg = new BitmapImage();
MemoryStream memStream = new MemoryStream(imageByteArray);
var randomAccessStream = memStream.AsRandomAccessStream(); //This line has error.
bitmapImg.SetSourceAsync(randomAccessStream);
return bitmapImg;
}
Can someone help me?
Thanks.
Solution
You can fix your problem by using the full namespace:
var randomAccessStream =
System.IO.WindowsRuntimeStreamExtensions.AsRandomAccessStream(memStream);
As it's an extension method, you can call it the way the code shows.
OTHER TIPS
What is going on is that AsRandomAccessStream
exists in more than one namespace being in scope . The compiler can't know which one you are referring to. You have two options:
- Remove the namespace that you do not need that also contains
AsRandomAccessStream
- Specify the complete path to
AsRandomAccessStream
likeSystem.IO.WindowsRuntimeStreamExtensions.AsRandomAccessStream
My guess is that EventHandler.UI.Extensions.StreamExtensions.AsRandomAccessStream
was possibly added by the update and System.IO.WindowsRuntimeStreamExtensions.AsRandomAccessStream
is the one you were using already.
AsRandomAccessStream
is an extension method, and you can't cast a method to some namespace. So you can't do something like object.ExtensionMethod() from MyNameSpace.ExtensionMethods
or so, for as far I know... If it is actually possible, I would like to know myself! So you can only call this extension method like any other regular static class method.
Little example code never hurts:
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Example NewExample = new Example();
//NewExample.DoSomething(); //Ambiguous error
ExtensionClass1.DoSomething(NewExample); //OK
}
}
public class Example
{
}
public static class ExtensionClass1
{
public static void DoSomething(this Example A)
{
}
}
public static class ExtensionClass2
{
public static void DoSomething(this Example A)
{
}
}
}