Unable to use async/await in LINQPad - "Invalid token {after async} in .." - c#

I wanted to try the "new" async/await (yeah, I'm late to the party) so I fired up LINQPad 4.5.1 which is currently the latest non-beta. After switching to "C# Program" and input the following code, sans comment;
// "Invalid token 'void' in class, struct, or interface member declaration"
// - But compiles with `async` removed
async void foo () {
}
void Main()
{
foo();
}
But it .. "doesn't work" as indicated.
This error seems to indicate that LINQPad is using C#4 and not C#5 although the website does say "LINQPad supports everything in C# 5.0 and Framework 4.x" and others don't seem to have this problem.
What causes this error, and how can it be resolved?

Even if this is not the solution for this particular issue, it may come handy for people like me that arrived to this question looking a fix for a compile error with the Task<T> return type because I was missing a using. The way to add it on LinqPad is pressing F4 > Namespace Imports tabs and adding at the bottom of the list System.Threading.Tasks then press Ok.
You can also set this new list as the default for the new queries pressing Set as default for new queries
Other way to access this tab is by the top menu Query > Namespace Imports

Well, that was .. silly of me. The current machine had .NET 4 - not 4.5 - installed.
After installing .NET 4.5 "it works".

Related

Missing method 'instance void MYDLLNAME ADODB.Fields...'

Have rewritten some parts of the application from VB.Net to C#. Everything is working as expected for all but one user. This user is getting this error message:
Error: Missing method 'instance void MYDLLNAME ADODB.Fields::Append(string,valuetype ADODB.DataTypeEnum,int32,valuetype ADODB.FieldAttributeEnum,object)' from class 'ADODB.InternalFields'
Is there a component potentially missing on the end user machine and is there a way to solve this or is it best to go away from ADODB.Recordset to ADO.Net Dataset?
Have seen the following questions, but they do not really solve my problem. Not upgraded framework, was already 4.5, VS 2017 used.
Strange error appending fields to recordset in VS2010 after converting to .NET 4
https://social.msdn.microsoft.com/Forums/en-US/f84cdc9c-f684-46d6-9b6f-757d047b00d5/fieldsappend?forum=Vsexpressvb
P.S. Using COM is not an option in my case.
Problem solved by setting properties of reference to ADODB in the project to:
Embed Interop = false
Copy Local = true
These are inverted defaults.

Resharper with COM interop complains "Cannot access internal constructor"

I have an OCX file built using VB6 that I'd like to access from C#. So I added a reference to the OCX in a C# project, and then added code to instantiate an object from the OCX:
var blah = new Blah();
This compiles just fine, without errors or warnings, and it seems to work as I would expect - I can call methods on the blah object and they seem to do what I would expect them to do. However, if I go to ReSharper / Inspect / Code Issues in Solution, ReSharper complains that the above quoted line is a "C# Compiler Error", saying "Cannot access internal constructor 'BlahClass' here".
Since it's claiming it's a C# compiler error, yet it seems to compile (and in fact work) just fine, I'm guessing it's just an issue with ReSharper itself. However, I'm pretty new to this, and I'd like to make sure. Perhaps what I'm doing is not the correct way of instantiating an object from an OCX, or something like that?
I am using VS2012 Professional and ReSharper 7.1.3.
The C# compiler looks at that coe and sees invocation of "dynamic" code (code whose objects may have new methods/properties available after execution started) and thus can't be sure that any particular method/property isn't there at compile time. Resharper tries to help out a bit more by trying to figure out circumstances that might fail at runtime. It may be using an out-dated description (type library) of that class to make its decision. Or, that description of that class is out of sync with what the code actually does. It's hard to tell solely based on what you've posted whether that's a good thing or a bad thing. If "it works", it seems like a good thing; but that's very subjective--it could be a problem waiting to happen.
I would see this as suspicious and maybe contact the vendor to get reassurance there isn't something lurking in there that will cause problems later.

Prevent Resharper warning when declaring return of non-empty sequence via Contract.Ensures()?

I'm using Code Contracts to declare that a property returns a non-null, non-empty sequence of strings like so:
public IEnumerable<string> Filenames
{
get
{
Contract.Ensures(Contract.Result<IEnumerable<string>>() != null);
// Next line gives Resharper Warning
// "Possible null assignment to entity marked with 'not null' attribute":
Contract.Ensures(Contract.Result<IEnumerable<string>>().Any());
return new []{"TEST"}; // Dummy data for demo purposes.
}
}
I'm getting a warning from Resharper as described in the code comment above.
This is similar to the question here:, but I have tried applying the fix in the answer to that question and it doesn't fix this particular issue.
Does anyone know how to fix this (other than using Resharper comments to suppress the warning)?
I'm using Resharper 7.1.2 C# Edition, build 7.1.2000.1478
(I've checked on several machines and it happens on all of them. Vanilla install of R# - we haven't modified any of its XML files other than me trying to apply the fix from the answer that I linked above.)
Further information:
I'm trying this with Visual Studio 2012 with update 2, with .Net 4.0 and .Net 4.5.
Also, you need to add the conditional compilation symbol "CONTRACTS_FULL" to the project's Build settings (in the "conditional compilation symbol" textbox).
The problem is that although most of the Code Contracts are covered in ReSharper ExternalAnnotations, Ensures is not one of them (not even in any of the custom xmls floating around).
I just double checked ExternalAnnotations from the latest ReSharper v8 EAP, and they are still exactly the same as in the v7.1.3 - so absolutely nothing has changed so far.
I'll make a new question, asking if anyone knows how to implement it.
UPDATE: Code Contracts Ensures for ReSharper ExternalAnnotations
FINAL: it's not doable at all - simply as the attribute would somehow need to be implied for the method containing the code contract, and not what's inside the code contract itself...

How to Register a Service in Mac OS?

I am experimenting with Mono, MonoMac, and C#. I try to write a simple application based on a service for Mac OS. I followed Apple's developer documentation on implementation of service providers.
My problem is: how to register a service. In Objective-C I would call the setServicesProvider method of NSApplication. But it seems that there is no such a method in MonoMac. Is this right? In the mono docs I found that NSApplication is supposed to have a services provider with a corresponding setter. But calling the SetServicesProvider like:
MyClass myClass = new MyClass();
NSApplication.SetServicesProvider(myClass);
results in the following error:
Error CS0117: `MonoMac.AppKit.NSApplication' does not contain a definition for `SetServicesProvider' (CS0117)
What is the problem here? Is the ServicesProvider property part of the 10% of AppKit that is not implemented so far (The Status section on the MonoMac project page says under "AppKit (About 10% left to be done))?
Alternatively, I tried to find a class NSRegisterServicesProvider as suggested in the apple developer's example. But I cannot find such a class.
Anyone knows how to register a service using MonoMac? Could you please point me to a solution, an example?
I am using MonoDevelop 3.1.1 and Xamarin.Mac development add-in (Version 3.1.1). The latter is at least what is stated in the add-in manager. The version information tab in the "About MonoDevelop" says "Xamarin.Mac: Not Installed". But I think the latter is not right since I get for example access to the NSApplication class using MonoMac.AppKit.
I asked this question on the Mono - OSX mailing list but unfortunately I did get no reply so far.
This is wrapped by a property in Xamarin.Mac. Try;
NSApplication.SharedApplication.ServicesProvider = new myClass()
The hardest thing I have found with these services is getting the info.plist declaration correct.

Clay and Castle Windsor 2.5

I've just downloaded the dynamic object framework Clay and am running into issues regarding castle project versions. Clay uses functionality from v2.0 of "castle" whilst I have a project which has been started referencing v2.5. Needless to say just to make matters more interesting I'm a complete beginner in all things "Castle" and IoC.
The real problem is that upgrading the references within clay solution results in a depreciated method warning. Regardless of whether you supress the method or not, the provided unit tests fail with a "Cannot perform runtime binding on a null reference" exception in the following code in "Intercept" of "InterfaceProxyBehavior":
var invoker = BindInvoker(invocation);
invoker(invocation);
The code that produces the run-time warning is in "CreateInstance" of "DefaultClayActivator":
//var proxyType = _builder.CreateClassProxy(baseType, options);
var proxyType = _builder.CreateClassProxyType(baseType, null, options);
As previously stated I'm still a complete beginner with Castle Windsor and just starting out with IoC so haven't even come across the Proxy stuff yet. Frustratingly I have no idea what the error message is even telling me, or asking for.
Have anyone already ported Clay across to version 2.5 of the castle project, so know the steps needed. Or can any one with experience of this part of castle throw anymore light on the error and what I may need to do to resolve it.
Updated
I'm still not really any the wiser as to the functionality that is failing, but have had chance to revisit the code running it both with v2.0 (works) and v2.5 (breaks) in castle.core. Attached are two images of the debug information when it works and then when it breaks. The test that it fails on is below, I've indicated the call with a comment.
namespace ClaySharp.Tests {
[TestFixture]
public class BinderFallbackTests {
...
[Test]
public void TestInvokePaths() {
var dynamically = ClayActivator.CreateInstance<Alpha>(new IClayBehavior[] {
new InterfaceProxyBehavior(),
new AlphaBehavior()
});
Alpha statically = dynamically;
IAlpha interfacially = dynamically;
Assert.That(dynamically.Hello(), Is.EqualTo("World-"));
Assert.That(statically.Hello(), Is.EqualTo("World-"));
Assert.That(interfacially.Hello(), Is.EqualTo("World-")); // <- Fails on this call
Assert.That(dynamically.Foo(), Is.EqualTo("Bar-"));
Assert.That(interfacially.Foo(), Is.EqualTo("Bar-"));
Assert.Throws<RuntimeBinderException>(() => dynamically.MissingNotHandled());
}
...
}
}
This is the debug information when using v2.5 of castle.core and the exception is thrown:
This is the debug information using v2.0 of castle.core (which works) for the same call / line that causes the problem with v2.5
It seems I fixed this. (all tests passing)
See the workitem on codeplex I created and the changes I pushed to my fork:
http://clay.codeplex.com/SourceControl/network/Forks/remcoros/Clay
I have never used this Clay thing, so all stuff below is based on assumptions.
The error message from BindInvoker is not a Castle error, but I'm guessing it's happening because the invoker is trying to bind to invocation target of the proxy which in DynamicProxy 2.1 used to have a value in some cases, which was wrong, and later versions 2.2 and 2.5 fixed that but it was a breaking change that you're now experiencing.
The other error message is telling you
Use CreateClassProxyType method
instead.
Which is the other method you commented out. What's not obvious here?
Some nice tutorials on Clay dynamic objects:
Malleable C# dynamic objects Part 1 Link
Malleable C# dynamic objects Part 2 Link.

Categories

Resources