LitJSON error in powershell - c#

I'm trying to use Leonardo B's litjson library from PowerShell. The entry point to the library I need to use is a static method. I can load the assembly with add-type, no problem. Trying to use the static method though gives this error:
PS C:\Users\david>[litjson.jsonmapper]::toobject("{`"foo`":`"bar`"}")
format-default : The JsonData instance has to be initialized first
+ CategoryInfo : NotSpecified: (:) [format-default], InvalidOperationException
+ FullyQualifiedErrorId : System.InvalidOperationException,Microsoft.PowerShell.Commands.FormatDefaultCommand
I though that what was happening was that the static constructor for this class didn't run, but Lars Truijens corrected that misunderstanding. The error is an exception from the JsonData class. Although the same line of code works correctly in VS2012 (targeting .net 3.5) it won't run in powershell.
UPDATE:
This unit test passes in visual studio 2012:
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using LitJson;
namespace litjsonTest
{
[TestClass]
public class TestJsonMapper
{
[TestMethod]
public void TestMethod1()
{
var obj = LitJson.JsonMapper.ToObject("{\"foo\":\"bar\"}");
Assert.IsNotNull(obj);
}
}
}

That would totally break .Net. I doubt that. Even more since the error is from the JsonData class and is because a type field is not set from the (non static) constructor of that class. See http://couchbrowse.googlecode.com/svn/trunk/LitJson/JsonData.cs Are you sure your LitJson code is correct? Does this exact code work in a C# program?
UPDATE
Are you sure your Json is correct? The example you posted isn't. There should be double quotes. Try escaping use the back tick or use a here-string
[litjson.jsonmapper]::toobject("{`"foo`":`"bar`"}")

Related

My System.CommandLine app won't build! It can't find a CommandHandler. Do I need to write it?

I am using VS 2022, .Net 6.0, and trying to build my first app using System.CommandLine.
Problem: when I build it, I get an error
The name 'CommandHandler' does not exist in the current context
The code I'm trying to build is the sample app from the GitHub site: https://github.com/dotnet/command-line-api/blob/main/docs/Your-first-app-with-System-CommandLine.md , without alteration (I think).
It looks like this:
using System;
using System.CommandLine;
using System.IO;
static int Main(string[] args)
{
// Create a root command with some options
var rootCommand = new RootCommand
{
new Option<int>(
"--int-option",
getDefaultValue: () => 42,
description: "An option whose argument is parsed as an int"),
new Option<bool>(
"--bool-option",
"An option whose argument is parsed as a bool"),
new Option<FileInfo>(
"--file-option",
"An option whose argument is parsed as a FileInfo")
};
rootCommand.Description = "My sample app";
// Note that the parameters of the handler method are matched according to the names of the options
rootCommand.Handler = CommandHandler.Create<int, bool, FileInfo>((intOption, boolOption, fileOption) =>
{
Console.WriteLine($"The value for --int-option is: {intOption}");
Console.WriteLine($"The value for --bool-option is: {boolOption}");
Console.WriteLine($"The value for --file-option is: {fileOption?.FullName ?? "null"}");
});
// Parse the incoming args and invoke the handler
return rootCommand.InvokeAsync(args).Result;
}
I have installed the latest version of System.Commandline: 2.0.0-beta2.21617.1
SURELY I am just being a big fat idiot in some respect. But I don't see it.
Any insight would be welcomed.
This issue is caused by updating the CommandLine 2.0 Beta 2 package. Add the reference System.CommandLine.NamingConventionBinder to the references to fix the problem. Follow the announcements on command-line-api's GitHub account:
In your project, add a reference to System.CommandLine.NamingConventionBinder.
In your code, change references to the System.CommandLine.Invocation namespace to
use System.CommandLine.NamingConventionBinder, where the CommandHandler.Create
methods are now found. (There’s no longer a CommandHandler type in
System.CommandLine, so after you update you’ll get compilation errors until you
reference System.CommandLine.NamingConventionBinder.)
If you want to continue with the old habits, try using older versions of the System.CommandLine package.
References
Announcing System.CommandLine 2.0 Beta 2 and the road to GA
Think you are missing a using line:
using System;
using System.CommandLine;
using System.CommandLine.Invocation;
using System.IO;
I can't swear that's it, but it looks like CommandHandler is defined in a namespace not referenced by a using (in your current code), so System.CommandLine.Invocation may be the key!

Powershell vs C# EscapeUriString different results

I have the following problem when using standard C# code and command:
Uri.EscapeUriString("[]")
I get :
"[]"
but when I'm using PowerShell:
[Uri]::EscapeUriString("[]")
I get
"%5B%5D"
Could someone please explain me what the differences are in those method calls?
And why am I getting different results? How to force PowerShell to return the same results as standard c#?
Thanks in advance
This seems to be .NET version related. Your C# code probably uses a more recent version.
C#
// .NET 4.0
Uri.EscapeUriString("[]"); // OUTPUT: "%5B%5D"
// .NET 4.5
Uri.EscapeUriString("[]"); // OUTPUT: "[]"
PowerShell
# v5
[Uri]::EscapeUriString("[]") # OUTPUT: "%5B%5D"
# for reference
Add-Type "
using System;
namespace PowerShell
{
public static class Uri
{
public static string EscapeUriString(string stringToEscape)
{
return System.Uri.EscapeUriString(stringToEscape);
}
}
}"
[PowerShell.Uri]::EscapeUriString("[]") # OUTPUT: "%5B%5D"
There is no "Standard C#". The solution would be to make sure both are using the same .NET version, or implement your own escape method.
Alternatively, use System.Net.WebUtility.UrlEncode which seems to return "%5B%5D" in both cases.

System.Console resolving to incorrect namespace

I have a simple console app with the namespace of
something.service.console
The problem is when I try to use
Console.WriteLine("something");
I get compile error: The Type or namespace name "WriteLine" dos not exist in the namespace something.service.console.
So unless I use
System.Console.WriteLine("something");
The C# compiler is trying to resolve the method WriteLine to the incorrect namespace ("something.service.console").
In this scenario is it possible to force the compiler to resolve the Console.WriteLine to the correct namespace "System" (instead of renaming my namespace :))?
Thank you.
The compiler will find the namespace something.service before it finds the namespace System so it will assume that
Console.WriteLine("something");
actually means
something.serviceConsole.WriteLine("something");
and hence your error.
Two possible solutions are to either fully qualify the namespace when you have issues like this:
System.Console.WriteLine("something");
or change the name of your namespace so it's not something.service.console but something.service.somethinglese.
You could "force" it like this:
using SysConsole = System.Console;
Now whenever you use Console in is refering to System.Console
public class Console
{
private void Test()
{
SysConsole.WriteLine("something");
}
}
Note: There really is nothing bad about using:
System.Console.WriteLine()
and you should avoid using classnames that already exist in the .NET Framework.
Using a C# 6 feature "using static" you can change the code to in order to avoid the ambiguous name Console without cluttering the code.
This would make sense, if a lot of System.Console.WriteLine calls occur in the code.
using static System.Console;
namespace SomeNamespace.Console
{
public class SomeClass
{
public void SomeMethod()
{
WriteLine("abc");
}
}
}

Calling C# dll in vbscript

I am trying to call a C# dll from QTP (uses vbscript). I have tried a number of things with no success:
Visual Studio 2010
Create C# class libary (st.dll)
code:
using System;
using System.Collections.Generic;
using System.Text;
namespace st
{
public class Class1
{
public static int GetValue()
{
return 34;
}
}
}
regasm /codebase st.dll
fails 'because it is not a valid .NET assembly'
In QTP/vbscript, I have tried
extern.Declare micInteger, "GetValue", "e:\st.dll", "GetValue"
Returns message: 'Invalid procedure call or argument'
Regardless of QTP, I would greatly appreciate any insight on how to call the c# dll from a .vbs file.
I was able to get this working by doing the following:
Create a new C# dll in VS 2010.
namespace st4
{
public class st4_functions
{
public int GetValue()
{
return 34;
}
}
}
In QTP I added the following lines:
Set obj = DotNetFactory.CreateInstance("st4.st4_functions", "c:\\st4.dll")
MsgBox obj.GetValue()
Thanks to all that responded to my problem. Though I did not do the COM solution, it got me thinking that I could stay with .NET and led to this solution. Good job all!
EDIT:
I created a blog post to detail the steps and provide additional information:
http://www.solutionmaniacs.com/blog/2012/5/29/qtp-calling-c-dll-in-vbscript.html
As Marc said, but I think it merits an answer. If you ensure that your dll will be available though the COM mechanics, your script should be able to call into it with things like CreateObject.
How to register .NET assembly for COM interop
Your function is static. Static class members can't be matched up to interface members, and if it can't implement a .NET interface then it certainly won't implement a COM interface.

F# declared namespace is not available in the c# project or visible through the object browser

F# declared namespace is not available in the c# project or visible through the object browser.
I have built a normal F# library project, but even after i build the project and reference it to my C# project, I am unable to access the desired namespace.
I am also unable to see it in the object browser, i get an error telling me that it has not been built. I am running on the september release can someone point out my error ?
F# Version 1.9.6.0
(6) Edit : Referencing the dll directly has fixed my problem, referencing the project allows me to compile but the intellisence does not work. When the dll is directly referenced the intellisence works perfectly.
This is the code found in the .fs file
#light
namespace Soilsiu.Core
module public Process =
open System.Xml.Linq
let private xname (tag:string) = XName.Get(tag)
let private tagUrl (tag:XElement) = let attribute = tag.Attribute(xname "href")
attribute.Value
let Bookmarks(xmlFile:string) =
let xml = XDocument.Load(xmlFile)
xml.Elements <| xname "A" |> Seq.map(tagUrl)
let PrintBookmarks (xmlFile:string) =
let list = Bookmarks(xmlFile)
list |> Seq.iter(fun u -> printfn "%s" u)
(5) Edit : Could ReSharper 4.0 be the problem?
(4) Edit : When i say the Object browser is unable to read the resulting assembly, i mean that when I try to open the assembly in the object browser i get an error telling me the project has not yet been built. yet again i can read the assembly using reflector.
(3) Edit : Reflector can Disassemble the dll but the Object Browser is unable to read it.
(2) Edit : I have Upgraded my F# version to 1.9.6.2 and still the same consequence
(1) Edit : I was able to Disassemble the dll to C# I get : (Everything seems to be fine here)
namespace Soilsiu.Core
{
[CompilationMapping(7)]
public static class Crawler
[CompilationMapping(7)]
public static class Process
}
[CompilationMapping(7)]
public static class Process
{
// Methods
static Process();
public static IEnumerable<string> Bookmarks(string xmlFile);
public static void PrintBookmarks(string xmlFile);
internal static string tagUrl(XElement tag);
internal static XName xname(string tag);
// Nested Types
[Serializable]
internal class clo#13 : FastFunc<XElement, string>
{
// Methods
public clo#13();
public override string Invoke(XElement tag#9);
}
[Serializable]
internal class clo#17 : FastFunc<string, Unit>
{
// Methods
public clo#17();
public override Unit Invoke(string u);
}
}
What if you reference the produced DLL directly (i.e., not via a project reference, but via a file reference)?
Maybe IntelliSense is just messed up? What compiler error do you get when you try to use it in C#? When you say "the object browser is unable to read it" what does that mean?
For what it's worth, I added this to a F# library project, referenced it (project) from a C# console app, and was able to use it. IntelliSense did not work at first though. (Had to rebuild.)
If you can make a solid repro, I'd suggest emailing it to F# bugs alias (fsbugs).
I tried the same thing. It looks as if Visual Studio and Resharper 4.0 doesn't understand F# for some reason. If you ignore the sea of red text and the lack of intellisense, it will compile fine.
Try
Make sure that C# project is targeted FULL .NET (NOT Client Profile).
Add references to assemblies into C# project which are used by F# project.

Categories

Resources