How to fix ambiguous reference errors? - c#

I have a web app that allows importing of contacts from Hotmail, Yahoo and GMail. I finally have it almost completed but since I added the importing of GMail, I am getting ambiguous reference errors and I am unsure how to fix them without breaking any code.
Here is a screen shot of the errors:

Try to use unique class names as much as possible. This will be the better solution in the end.
Write the entire namespace when referencing
OAuth.OAuthBase a = new ...;
Google.GData.Client.OAuthBase b = new ...;
Make an using alias for one or both:
using n2 = OAuth;
using Google.GData.Client;
n2.OAuthBase a = new ...; // referenced using namespace
OAuthBase b = new ...; // referenced through existing `using`

you can try something like this..
using GoogleOAuthBase = Google.GData.Client.OAuthBase;
namespace abc
{
public class Program
{
//make sure this Google.GData.Client.OAuthBase is instansiateable
var googleBase = new GoogleOAuthBase();
}
}
you can try entire name space as well.
var googleBase = new Google.GData.Client.OAuthBase();

Related

Creating a script with List

I'm attempting to create a script with Microsoft.CodeAnalysis.CSharp.Scripting.
As soon as I add List<> the code errors. I thought I had all the necessary references and usings included, however it still errors stating The type or namespace name 'List<>' could not be found (are you missing a using directive or an assembly reference?
These are my usings in the code
using Microsoft.CodeAnalysis.CSharp.Scripting;
using Microsoft.CodeAnalysis.Scripting;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Diagnostics;
Below is my example unit test
[TestMethod]
public void RunGenericListTest()
{
var code = #"
List<string> strings = new List<string>();
strings.Add(""test string"");
return output;";
var options = ScriptOptions.Default;
options.WithReferences(typeof(System.Collections.CollectionBase).Assembly);
options.WithReferences(typeof(System.Collections.Generic.List<>).Assembly);
options.WithImports("System.Collections");
options.WithImports("System.Collections.Generic");
var result = CSharpScript.RunAsync(code, options).Result;
Debug.WriteLine(result);
}
This errors on the CSharpScript.RunAsync every time. Can someone enlighten me on what I'm missing?
I think the issue is, WithImports does not mutate options but rather returns a copy
var code = #"
List<string> strings = new List<string>();
strings.Add(""test string"");
return strings;";
var options = ScriptOptions.Default
.WithImports("System.Collections.Generic"); // chaining methods would work better here.
// alternatively reassign the variable:
// options = options.WithImports("System.Collections.Generic");
var result = CSharpScript.RunAsync(code, options).Result;
Debug.WriteLine((result.ReturnValue as List<string>).First());

Parase XML data from NMAP output using c# and this persons library NmapXmlParser

The developer of this particular library seems to be MIA.
https://github.com/Kamiizumi/NmapXmlParser
To test the issue grab his library (NmapXmlParser) from Nu-Get and make sure you have
using System.Xml.Serialization;
using System.IO;
using Xunit;
The code he gives on the example looks like this
var xmlSerializer = new XmlSerializer(typeof(nmaprun));
var result = default(nmaprun);
using (var xmlStream = new StreamReader("NmapResults.xml"))
{
result = xmlSerializer.Deserialize(xmlStream) as nmaprun;
}
Console.WriteLine(result.args);
This works when getting the elements inside of the nmaprun object.
He does not give any other examples so I assumed if I wanted to check the host object i would change all instances of nmaprun in above code to host. And then on the Console line change to an element inside the host object like this
var xmlSerializer = new XmlSerializer(typeof(host));
var result = default(host);
using (var xmlStream = new StreamReader("NmapResults.xml"))
{
result = xmlSerializer.Deserialize(xmlStream) as host;
}
Console.WriteLine(result.reason);
The Intellisense inside of the Console.Writeline wants to autocomplete to the elements so I feel its set up right but I keep getting this error.
System.InvalidOperationException: 'There is an error in XML document (5, 2).'
Inner Exception InvalidOperationException: was not expected.
You can use the example XML in his Github if you do not have an Nmap xml output file

c# Cannot create InlineKeyboardButton

I am creating a telegram bot, but I am unable to create any InlineKeyboardButton objects.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Telegram.Bot.Types.InlineKeyboardButtons;
namespace BuildAutomation.Controllers
{
public class Test
{
public TestMethod()
{
var none = new InlineKeyboardButton("No", "build|no");
var yes = "Yes";
var betaControl = new InlineKeyboardButton(yes, "build|betacontrol");
var betaNode = new InlineKeyboardButton(yes, "build|betanode");
var betaBoth = new InlineKeyboardButton(yes, "build|betaboth");
InlineKeyboardMarkup menu;
menu = new InlineKeyboardMarkup(new[] { betaBoth, none });
}
}
}
I keep getting the error 'Cannot create an instance of the abstract class or interface 'InlineKeyboardButton'. I realize that InlineKeyboardButton is an abstract class, but i see many examples creating an object of InlineKeyboardButton.
Did I miss something?
example 1
example 2
example 3
Instantiation of InlineKeyboardButton directly was correct in previous versions.
There is a new commit for about one month ago which indicates that InlineKeyboardButton is made abstract from then on. You must use derived classes instead. InlineKeyboardUrlButton, InlineKeyboardPayButton and etc. are all derived from InlineKeyboardButton.
It seems that the examples' repository is not updated yet.
Check this link for more details about the mentioned commit:
https://github.com/TelegramBots/telegram.bot/commit/ddaa8b74e3ab5eab632dbe2e8916c2fe87b114a3
Use InlineKeyboardCallbackButton Example, InlineKeyboardCallbackButton("Yes", "CallBackData") instead of InlineKeyboardButton("Yes", "CallbackData")
You couldn't create a new instance of abstraction class. So use sample bellow
var KeyboardButons = new InlineKeyboardButton[][]
{
new InlineKeyboardButton[]
{
InlineKeyboardButton.WithCallbackData("سفارش", callbackQueryData) ,
InlineKeyboardButton.WithCallbackData("بازگشت", "return")
}
};
var replyMarkup = new InlineKeyboardMarkup()
{
InlineKeyboard = KeyboardButons
};

fakeXrmEasy for crm testing initialization issues

I'm trying to follow the basic tutorial for FakeXrmEasy, but I'm not sure why I'm getting errors. I installed everything that needs to be installed to mock Dynamics 365, but I'm still getting errors. I can't figure out what I'm missing, I really want to be able to use this tool.
CS1950 The best overloaded Add method 'List.Add(Entity)' for the collection initializer has some invalid arguments unitTest c:\Users\acapell\documents\visual studio 2015\Projects\unitTest\unitTest\Program.cs 48 Active
CS0246 The type or namespace name 'Account' could not be found (are you missing a using directive or an assembly reference?) unitTest c:\Users\acapell\documents\visual studio 2015\Projects\unitTest\unitTest\Program.cs 45 Active
Didn't know if I was suppose to create an account class, I also tried that but that didn't work either. I got
CS1503 Argument 1: cannot convert from 'unitTest.Account' to 'Microsoft.Xrm.Sdk.Entity' unitTest c:\Users\acapell\documents\visual studio 2015\Projects\unitTest\unitTest\Program.cs 48 Active
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;
using FakeItEasy;
using FakeXrmEasy;
using Microsoft.Xrm.Sdk;
namespace unitTest
{
class Program
{
static void Main(string[] args)
{
}
}
class unitTest
{
public object ProxyTypesAssembly { get; private set; }
public void MyFirstTest()
{//test method body
var context = new XrmFakedContext();
//You can think of a context like an Organisation database which stores entities In Memory.
//We can also use TypedEntities but we need to tell the context where to look for them,
//this could be done, easily, like this:
context.ProxyTypesAssembly = Assembly.GetAssembly(typeof(Account));
//We have to define our initial state now,
//by calling the Initialize method, which expects a list of entities.
var account = new Account() { Id = Guid.NewGuid(), Name = "My First Faked Account yeah!" };
context.Initialize(new List<Entity>() {
account
});
}
}
}
Do you use early binding in your CRM project and got the references right? If you do not use early binding, you can try late binding, e.g.
//context.ProxyTypesAssembly = Assembly.GetAssembly(typeof(Account));
var account = new Entity();
account.LogicalName = "account";
account.Attributes["name"] = "your account name";

Roslyn / Find References - Can't properly load Workspace

I'm trying to write some code to find all method invocations of any given method as I am looking to create an open source UML Sequence Diagramming tool. I'm having trouble, however, getting past the first few lines of code :/
The API appears to have changed drastically and I can't seem to infer proper usage by looking at the code.
When I do:
var workspace = new CustomWorkspace();
string solutionPath = #"C:\Workspace\RoslynTest\RoslynTest.sln";
var solution = workspace.CurrentSolution;
I find that workspace.CurrentSolution has 0 Projects. I figured this would be the equivalent to what was previously Workspace.LoadSolution( string solutionFile ) which would then supposedly contain any Projects in the Solution, but I am not finding any success with this path.
I am terribly confused 0.o
If someone could offer some additional guidance as to how I can use the FindReferences API to identify all invocations of a particular method, it would be very much appreciated!
Alternatively, would I be better off taking a static-analysis approach? I would like to support things like lambdas, iterator methods and async.
====================================================================
Edit -
Here is a full example based on the accepted answer:
using System.Linq;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.MSBuild;
using Microsoft.CodeAnalysis.FindSymbols;
using System.Diagnostics;
namespace RoslynTest
{
class Program
{
static void Main(string[] args)
{
string solutionPath = #"C:\Workspace\RoslynTest\RoslynTest.sln";
var workspace = MSBuildWorkspace.Create();
var solution = workspace.OpenSolutionAsync(solutionPath).Result;
var project = solution.Projects.Where(p => p.Name == "RoslynTest").First();
var compilation = project.GetCompilationAsync().Result;
var programClass = compilation.GetTypeByMetadataName("RoslynTest.Program");
var barMethod = programClass.GetMembers("Bar").First();
var fooMethod = programClass.GetMembers("Foo").First();
var barResult = SymbolFinder.FindReferencesAsync(barMethod, solution).Result.ToList();
var fooResult = SymbolFinder.FindReferencesAsync(fooMethod, solution).Result.ToList();
Debug.Assert(barResult.First().Locations.Count() == 1);
Debug.Assert(fooResult.First().Locations.Count() == 0);
}
public bool Foo()
{
return "Bar" == Bar();
}
public string Bar()
{
return "Bar";
}
}
}
CustomWorkspace is
A workspace that allows manual addition of projects and documents.
Since you're trying to load a solution, you should use the MSBuildWorkspace, which is
A workspace that can be populated by opening MSBuild solution and project files.
You can create a new MSBuildWorkspace and call OpenSolutionAsync with your solutionPath. For the reference finding part, take a look at the SymbolFinder.
Solutions are an MSBuild concept.
You need to create an MSBuildWorkspace and call OpenSolutionAsync().
string solutionPath = #"C:\Workspace\RoslynTest\RoslynTest.sln";
creates a local variable. It has no influence on your CustomWorkspace object.

Categories

Resources