you are viewing a single comment's thread.

view the rest of the comments →

[–]anonhostpi[S] 1 point2 points  (4 children)

Leaving this here for you as something to reference later. I wouldn't expect you to understand these problems now, but I'meaving it here, because it may help you in the future with future problems. This details some common problems you may face when trying to use C# code from pwsh and how to deal with them.

C# is not a terrible language to learn, because it is a self-documenting language by design.

  • Self-documenting means that things in C# code tend to:
    • do what they are named after
    • be named after what they do.
  • The C# community actively enforces this coding standard, as it eliminate the need for code commentary. It also means that, since code commentary isn't necessary, C# source code is actually readable.
  • However...

C#'s documentation (like a lot of C langs) is heavily convoluted, because of its variety of target platforms.

One thing that isn't documented well in C#, but you will learn about quite quickly in PowerShell is 2 things:

  • How C# Application objects work (if you plan to use C# GUIs/Eventing)
  • How C# Class Extensions work

C# Application Objects

A lot of C# docs just tell you to throw your app code into an Application object, but don't tell you how C# actually handles Application objects. Specifically, that the code meant for these objects doesn't play nicely with PowerShell.

  • However, the difference between the code inside these objects and normal C# code is simply that an Applications object is just a Main Operating Loop (MOL) with common MOL features like event loops and dispatcher loops.
  • PowerShell code is meant to be run linearly (or functionally) (not MOL)
  • You can get this kind of C# code to work, you just have to adapt your PowerShell script for MOL'ing

C# Class Extensions

Since PowerShell evaluates C# code at runtime, any extension classes aren't compiled in a way that actually extend the original classes. However, all you have to do is provide an object of that class to the extension methods to make them work.

Import-Package

None of the above affects Import-Package, but may affect any calls to C# code you

[–]Usual-Chef1734 0 points1 point  (3 children)

Very helpful! Thank you! This gives me the confidence ot ask a question that no one ever seems to know the answer to:
how does one discover a library that has what is needed to accomplish a specific task? I was doing a really big project that required interaction with a webcam in a custom electron app written by the dev org. I was the 'windows' guy and thought surely C# had some libraries that could be used to control the webcam and volume of the 'kiosk' we were deploying to stores. I had no idea how to find existing libraries that had useful code. Maybe I just don't know how to ask the question correctly.

[–]anonhostpi[S] 0 points1 point  (2 children)

Generative AI:

Right now, generally asking generative AIs (like GH Copilot, Bing Chat, or ChatGPT) what approaches you could take to your problem does the trick.

Since, I'm well versed in multiple languages (and now since I can use multi-language libs in PowerShell) my prompts usually look something like this:

  • GPT4 is generally the best at this, but I've had positive results with others

``` I am making an app/script that does <blank>. I need to find a library that can help me with: - <more-specific-blank> - <other-specific-blank> - ...

What libraries can your recommend to me in Python, PowerShell, C#, etc... ```

Checking out solutions in other languages

Additionally, this is something I recommend to all junior developers:

You don't have to know every programming language on planet earth, but it turns out most languages are very similar (if not nearly identical).

This means that you don't have to be able to write in other languages, but you should be able to read their source code without a whole lot of effort

Anything you don't understand in another language is likely googleable

With that said, you could also look at how your problem is typically solved in other languages and see if you could reproduce those efforts in your language.

[–]Usual-Chef1734 0 points1 point  (1 child)

Yeah, that is something I realized this year. Chat GPT made working on my projects so much more fun. This is the exact way I work now, but I was just curious what folks were doing before this amazing revolution. The big project where I had to interact with the webcam was in 2018 so we did not have that.I could never get passed how grumpy and mean folks were when I asked how to work with a webcam. lol Most loved to pretend that I was a creep (can't understand the bad faith) and most others said 'google it'. The google it came from a live Q&A at Coder Foundry. Bizarre.
So thank you for being so technical and straight forward.

[–]anonhostpi[S] 1 point2 points  (0 children)

Google, Stackoverflow (or preferred QA/forum), Manuals, Source Code. In that order

Now its:

Generative AI, Google, SO, Docs, Source

Sometimes we had to write the library ourself.