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.
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.
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 like System.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)
{
}
}
}
Related
the C# harmony documentation: https://github.com/pardeike/Harmony/wiki/Prioritiy-annotations
my question is that not able to run the C# harmony example successfully
the postfix annotation didn't get injection log printed as expected after Class and method get patched that i didn't see "injection logs" get printed.
c# code example below. Can someone help me find the issue
you may paste into https://dotnetfiddle.net/ to debug it.
using System;
using System.Collections.Generic;
using System.Reflection;
using Harmony;
public class Program
{
public static void Main()
{
var harmony = HarmonyInstance.Create("net.example.plugin");
harmony.PatchAll(Assembly.GetExecutingAssembly());
Program p = new Program();
p.Bar();
}
[HarmonyPostfix]
[HarmonyPatch(typeof(Program), "Bar")]
public static void Postfix_Bar(){
Console.WriteLine("postfix Bar log"); // this never gets printed as expected.
}
[HarmonyPostfix]
[HarmonyPatch(typeof(Program), "Foo")]
public static void Postfix_Foo(ref string res){ //however, this gets error res could not be found. https://github.com/pardeike/Harmony/wiki/Prioritiy-annotations
Console.WriteLine("postfix Foo log");
res = "new value";
}
public void Bar() {
Console.WriteLine("Hello World !!! ");
}
static string Foo()
{
return "secret";
}
}
The main problem is that PatchAll() looks for classes that have at least on [HarmonyPatch] annotation. Your patches are in the class Program which does not have that annotations. This is the main problem in your example.
Solution: either annotate your Program class like this:
[HarmonyPatch]
public class Program
{
}
or create a new class that has that annotation.
The second problem I can see is that Postfix_Foo(ref string res) uses res which does not follow the documented standard for naming patch arguments. It can either be the name of an argument that the original method has (it has no arguments) or refer to the result, which requires it to be named __result.
The comment about priority annotations is misplaced too because they only apply to multiple patches to the same original method.
Finally, you call Bar() after patching which means that Foo is never called - not sure if that’s intended.
I have a static ExceptionHelper that looks like this:
public static class ExceptionHelper
{
public static async void ShowDialog(string message)
{
// Show message
}
}
Whenever I want to call this method I do it like this at the moment:
ExceptionHelper.ShowDialog("This is a message.");
I now thought of defining an alias for the ExceptionHelper to not having to write the whole word each time I want to use it.
I know I can achieve it with using:
using Ex = MyNamespaces.ExceptionHelper;
But then I'd have to define it in each file I want to use the method. Is there a way I can define the alias globally without changing the name of the class? Or is there any attribute I can set above the class declaration?
Extension Method
You could make it an extension method on string.
public static class ExceptionHelper
{
public static async void ShowDialog(this string message)
{
// Show message
}
}
Then you would use it like so:
using WhateverNamespaceExceptionHelperLivesIn;
public class TestClass
{
public void TestMethod()
{
"This is a message".ShowDialog();
}
}
This makes your question moot - you don't have to define an alias at all.
Static imports
An alternative approach is to import the class statically. You won't need an alias, because you can reference the ShowDialog method directly. This will require C#6/Visual Studio 2015.
using static WhateverNamespaceExceptionHelperLivesIn.ExceptionHelper;
public class TestClass
{
public void TestMethod()
{
ShowDialog("This is a message");
}
}
In C# 6.0 you can use static usings:
using static MyNamespace.ExceptionHelper;
Of course not globally, that works only for defines. But in a file where you use this line, you can use the members of the ExceptionHelper without any prefix.
As of C# 10, you can now define gloabl usings.
// GlobalUsing.cs
global using static WhateverNamespaceExceptionHelperLivesIn.ExceptionHelper;
And it will now be available globally, without having to define the class name, or the namespace, at the top of each class.
// Available Globally in the Project
public class TestClass
{
public void TestMethod()
{
ShowDialog("This is a message");
}
}
This might apply, even though you are using a method. You could use an ENUM type instead that lies outside of any namespace and access globals values that way. Place the enum in a file outside of any namespace. You can access it globally that way, or if you have trouble, using the "global" keyword below if you have any trouble referencing it:
enum Size
{
SMALL = 1,
MEDIUM = 5,
LARGE = 10
}
class Test {
int mysize1 = (int)Size.SMALL;
int mysize2 = (int)global::Size.MEDIUM;
}
I have a class:
namespace FooIOS
{
public class Foo
{
public static void doThis() {...}
}
}
And this works:
using FooIOS;
namespace Sample.iOS
{
public void method () {
Foo.doThis();
}
}
However, this does not work the same way when I change the namespace to insert a period:
namespace Foo.iOS
{
public class Foo
{
public static void doThis() {...}
}
}
using Foo.iOS;
namespace Sample.iOS
{
public void method () {
// Compilation error
Foo.doThis();
// Compilation error
Foo.iOS.doThis()
// This works but I can't have it that long and complicated (I'm writing an API call)
Foo.iOS.Foo.doThis();
}
}
I'm pretty inexperienced with C# and I'm wondering if there's any way to use the period in the namespace and not deal with the complicated call.
namespace Foo.iOS
{
public class Foo
{
public static void doThis() {...}
}
}
Your namespace name is Foo.iOS, class name is Foo, static method name is doThis(). The fully qualified path to access that method is NAMESPACE.CLASS.METHOD_NAME, so it becomes:
Foo.iOS.Foo.doThis();
Here is nothing wrong with C#, but with the naming you use.
From this a couple of suggestions:
try to no use . inside names of the namespace, as this introduces confusion
try to not name namespace as the class inside it, as this introduces confusion.
I'm pretty inexperienced with C# and I'm wondering if there's any way
to use the period in the namespace and not deal with the complicated
call.
Short answer is: name your namespaces, classes and member functions in a way, that it does not look complicated to you and to others.
EDIT
Consider that you can use also Namespace Alias.
For example:
using IOS = Foo.iOS;
...
IOS.Foo.doThis();
But as I said before, it's better to avoid . in namespace name at first place.
Bring the using Foo.iOS; statement inside the namespace Sample.iOS namespace block, like shown below, then you will be able to call doThis() like in your 1st attempt Foo.doThis(); that was previously giving you a compile error.
namespace Sample.iOS
{
using Foo.iOS;
public void method () {
// this works
Foo.doThis();
}
}
Related reading: Inside or Outside? by Eric Lippert on MSDN.
Fully working Code sample:
Create a new Console App in Visual Studio, and then in the Program.cs class, delete all lines, paste the following, do a compile and then run.
using System;
namespace Foo.iOS
{
public class Foo
{
public static void doThis() { Console.Write("Inside doThis");}
}
}
namespace Sample.iOS
{
using Foo.iOS;
class Program
{
static void Main(string[] args)
{
method();
Console.ReadKey();
}
public static void method ()
{
// works fine
Foo.doThis();
}
}
}
I have some code base which has is calling the following:
SetHazardDataService();
namespace Analytics.Foo.DataServices
{
class HDB:IDataService
{
}
}
With a member function declared in another class/file
using Analytics.Foo.DataServices
public void MyDataService()
{
var DbDataSvc = new HDB();
}
originally, I see the same definition used elsewhere but with (no idea if that works):
protected void MyDataService()
I included the public method in my class
I'm now trying to recreate that functionality, but I get the following issue:
The type Analytics.Foo.DataServices.HDB' has no constructors defined
I'm not sure what the issue is - any suggestions for why this is the case. There is no constructor that I can see. Plus I'm not able to see the other code working/but it doesn't give the same issue.
You need to create a constructor to class HDB, like this:
namespace Analytics.Foo.DataServices
{
class HDB:IDataService
{
public HDB()
{
}
}
}
'Main.mainClass' does not contain a
definition for 'myMethod' and no
extension method 'myMethod' accepting
a first argument of type
'Main.mainClass' could be found (are
you missing a using directive or an
assembly reference?)
Perhaps you have a static Main method and your "myMethod" is not static, in this case you should make either "myMethod" static or get an instance of mainClass.
class mainClass
{
public static void Main(string[] args)
{
mainClass main = new mainClass();
main.myMethod();
}
public void myMethod()
{
//Stuff here
}
}
or:
class mainClass
{
public static void Main(string[] args)
{
myMethod();
}
public static void myMethod()
{
//Stuff here
}
}
Without showing us your code it's impossible to tell exactly what the problem is but I would bet on one of these:
You are trying to access a static method using a member instance or the other way around
You have implemented a method with the same name but it has different parameters
You have made your method private and try to access it from another class
You tried to call a member method on yourObj like myMethod(yourObj) instead of yourObj.myMethod()
I think you are using referance to call that method.If yes, try to build just your project which contains that method, then rebuild solution. This problem occurs sometimes, when your build order fails.
We need more details to help you.