I am trying to add a type in powershell from c# code that uses an observablecollection. I've tried for a day or two and I can't get anything to work.
What I was trying to come up with was a view model for wpf without relying on any kind of extra powershell modules.
This is the latest I've come up with:
$code = #"
using System;
using System.Collections.ObjectModel;
namespace Test
{
public class TestClass
{
public ObservableCollection<string> Items { get; set; }
public TestClass()
{
Items = new ObservableCollection<string>();
}
}
}
"#
Add-Type -TypeDefinition $code -ReferencedAssemblies #("WindowsBase") -Language CSharpVersion3
I can't get past an error of:
Add-Type : c:\Users\ncollier\AppData\Local\Temp\wjq4ciwc.0.cs(9) : The type or namespace name 'ObservableCollection' could not be found (
are you missing a using directive or an assembly reference?)
Edit
I'm trying to run this in powergui, but if I run it in the powershell ISE maybe it works... Is there something I could be doing wrong in powergui?
try just :
Add-Type -TypeDefinition $code
In my box I can add type.
Edit after comment:
I discovered that using powershell.exe.config ( or for ISE powershell_ise.exe.config) to load .net 4.0 there's no needs to add ReferencedAssemblies (in this code, others situations needs reference system.core or may needs others assembly references ). I suspect that also PowerGui load .net 4.0 but I can test it.
You can try on PowerGui this:
[appdomain]::currentdomain.getassemblies()
and see if there are assemblies version 4.0.xxxx
Related
EDIT: Thank you everyone! I have never upgraded to a newer version of
.NET and language version before. Thus didn't know about .csproj
configuration. Even though I did a research before posting a question
I was not able to find a solution myself. So, I just leave these two
links for further reference, perhaps this might help someone as well.
https://learn.microsoft.com/en-us/dotnet/standard/frameworks
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/configure-language-version
I have upgraded to .NET 5.0.301
And finally got around to try record type in C# 9.0
I wrote a simple code but got an error during compilation.
I use Visual Studio Code as an editor.
VS Code version 1.57.0
C# extension version 1.23.12
Here is my settings.json:
"editor.semanticHighlighting.enabled": true,
"csharp.semanticHighlighting.enabled": true,
"omnisharp.path": "latest"
Set up:
dotnet new sln -n "Test"
dotnet new console -n "TestProject"
dotnet sln Test.sln add .\TestProject\TestProject.csproj
My code:
using System;
namespace TestProject
{
class Program
{
static void Main(string[] args)
{
var person = new Person() { Name = "Tom" };
person.Name = "Bob";
Console.WriteLine(person.Name);
}
}
public record Person
{
public string Name { get; set; }
public int Age { get; set; }
}
}
Problems:
CS0246 The type or namespace name 'Person' could not be found (are you missing a using directive or an assembly reference?)
CS0246 The type or namespace name 'record' could not be found (are you missing a using directive or an assembly reference?)
CS0548 '<invalid-global-code>.Person': property or indexer must have at least one accessor
CS1513 } expected
CS0116 A namespace cannot directly contain members such as fields or methods
CS1022 Type or namespace definition, or end-of-file expected
Any help much appreciated
Check your target framework and language version in your .csproj file. You should find something like:
<TargetFramework>net5.0</TargetFramework>
<LangVersion>9.0</LangVersion>
If you don't find these properties, add them inside the <PropertyGroup>...</PropertyGroup> tags. If they are set to older versions, change them.
If this does not solve your problem, you may have installed your .NET SDK incorrectly, or you may have installed it in a directory other than the default one and the VS Code C# extension is not able to find it.
I am trying to compile some c# code in linux, i can compile the same code in Windows using the csc command in Visual Studio command line. I also have already installed mono-devel.
this is the exact error :
mycode.cs(8,22): error CS0234: The type or namespace name `Forms' does not exist in the namespace `System.Windows'. Are you missing `System.Windows.Forms' assembly reference?
mycode.cs(70,27): error CS0246: The type or namespace name `Form' could not be found. Are you missing an assembly reference?
Why is this happening and how to fix it?
this is the command I'm using to compile it in linux :
mcs mycode.cs
and inside the code, Windows forms is already included :
using System.Windows.Forms;
also simple hello world code does get compiled without any error, i basically followed this blogpost about how to compile c# code in linux to set everything up :
https://jonsson.xyz/2016/11/23/csharp-linux/
Have you tried compiling with -r:System.Windows.Forms.dll ?
for example:
wc.cs:
using System.Windows.Forms;
public class Program
{
[STAThread]
public static void Main()
{
var f = new Form();
f.Text = "Hello World";
Application.Run(f);
}
}
then run:
$ mono-csc wf.cs -r:System.Windows.Forms.dll
and you get wf.exe
I am trying to get a very simple script to work in PowerShell, and it keep popping back with the following error:
Add-Type : Cannot add type. Definition of new types is not supported in this language mode.
At C:\Users\jdkin_000.ATHENA\cs_init.ps1:16 char:1
+ Add-Type -TypeDefinition $Source -Language CSharp
Here is the script:
$Source = #"
using System;
namespace cs1
{
public class Program
{
public static void Main()
{
Console.WriteLine("Hello world!");
}
}
}
"#
Add-Type -TypeDefinition $Source -Language CSharp
Any suggestions on how to get this script to work on the exact device that is throwing back the error. The script and similar scripts work on other machines, but when the script contains C#, this machine just doesn't want to play fair.
Try running this:
$ExecutionContext.SessionState.LanguageMode
Here is the link explaining the LanguageMode. I assume you are in a mode that restricts the ability to define new types.
https://technet.microsoft.com/en-us/library/dn433292.aspx
Edit: Based on the Windows RT 8.1 OS, (
Windows RT Powershell (PermissionDenied) on New-Object)
It doesn't appear that it can be done.
Powershell also may switch to ConstrainedLanguage mode when there is no space available on system drive.
I created a solution called Foo.
Added a class library called Foo.Common
Added a console app to call the library code from called ConsoleApp.
I referenced the Foo.Common from ConsoleApp and typed :
using Foo.Common;
public class Program
{
CommonClass c = new CommonClass();
static void Main(string[] args)
{
}
}
and get this back :
Error 1 The type or namespace name '**Foo**' could not be found (are you missing a using directive or an assembly reference?) Z:\Foo\Solution1\ConsoleApplication1\Program.cs 3 11 ConsoleApplication1
Why am i getting this?
what s going on?
Make sure that
The ConsoleApp project has a reference to the Foo.Common project (do not browse for Foo.Common.dll),
the file contains a using directive for the namespace in which CommonClass is declared, and
CommonClass is declared as public.
So your files should look like this:
CommonClass.cs in Foo.Common project:
namespace Foo.Common
{
public class CommonClass
{
public CommonClass()
{
}
}
}
Program.cs in ConsoleApp project:
using Foo.Common;
namespace ConsoleApp
{
public class Program
{
public static void Main()
{
CommonClass x = new CommonClass();
}
}
}
Ensure that under your project settings, the target framework is set as .NET Framework 4 and not .NET Framework 4 Client Profile. I got this same behavior when it was set to Client Profile and it was fixes as soon as I set it to just the regular .NET Framework 4.
Right Click on the new console app solution/project and Add Reference and add the project that contains the Foo namespace
Did you add a reference to the library? Look under "References" in the console project. If its not there, you need to add it.
I posted this as a comment, but I want to expand on it here. What's probably happening is it's seeing using as a statement and not a keyword. It appears you have something like the following:
using System;
namespace TestNamespace
{
using Foo.Common;
public Class { }
}
Try
using System;
using Foo.Common;
namespace TestNamespace
{
public Class { }
}
Instead.
It looks like Foo Bar got this error because his project's target framework was set to the client profile.
Just thought I'd add one more 'solution' -- I created a library that targeted the 4.5 framework. My older project was tarting the 4 framework. I got this error.
Changing the older project to 4.5 made it work.
I've made a c# class that I'm trying to run in PowerShell with add-Type.
I have a few of my own assemblies referenced and some from .net4. I'm using my own PowerShell host because the assemblies I'm referencing are made on the .net4 framework and the PowerShell Console doesn't support that yet.
Here is a sample of the script that i'm trying to run:
$Source = #"
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using MyOwn.Components.Utilities;
using MyOwn.Components;
using MyOwn.Entities;
public class ComponentSubtypeMustMatchTemplate
{
public static Guid ProblemId
{
get { return new Guid("{527DF518-6E91-44e1-B1E4-98E0599CB6B2}"); }
}
public static ProblemCollection Validate()
{
SoftwareStructureEntityContainer softwareEntityStructure = new SoftwareStructureEntityContainer();
if (softwareEntityStructure == null)
{
throw new ArgumentNullException("softwareStructure");
}
ProblemCollection problems = new ProblemCollection();
foreach (var productDependency in softwareEntityStructure.ProductDependencies)
{......
return problems;
}
}
"#
$refs = #("d:\Projects\MyOwn\bin\MyOwn.Components.dll",
"d:\Projects\MyOwn\bin\MyOwn.Entities.dll",
"d:\Projects\MyOwn\bin\MyOwn.OperationalManagement.dll",
"c:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Core.dll",
"c:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Data.Entity.dll",
"c:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Data.dll")
Add-Type -ReferencedAssemblies $refs -TypeDefinition $Source
$MyProblemCollection = new-Object ComponentSubtypeMustMatchTemplate
$MyProblemCollection.Validate()
Now when i run this I get this error:
{"Method invocation failed because [ComponentSubtypeMustMatchTemplate] doesn't contain a method named 'Validate'."}
Ive also tried the last bit in different ways (found many different examples on how to do this) but they all seem to give the same error. I really have no idea how to get this working, and i can't seem to find any example's on something similar.
On a second note(for when i get this fixed) I was wondering if it was possible to be able to just load the .cs file instead of putting the c# code in the script. would be a better read and more maintainable.
Ive tried to see if I can see the methods with Get-Member -static like this:
$MyProblemCollection = New-Object ComponentSubtypeMustMatchTemplate
$MyProblemCollection | Get-Member -Static
And it does see the method there:
TypeName: ComponentSubtypeMustMatchTemplate
Name MemberType Definition
---- ---------- ----------
Equals Method static bool Equals(System.Object objA, System.Obje...
ReferenceEquals Method static bool ReferenceEquals(System.Object objA, Sy...
Validate Method static MyOwn.ComponentSubtypeMustMatchTemplate... <--
ProblemId Property static System.Guid ProblemId {get;}
So why doesn't it work!?
#Philip
in my script it doesn't really matter if its static or non static cause it will always just be run once and the results will be used my program. But strange thing is it does seem to work if i use the static syntax (except for that i get no results at all) and if i use the non-static syntax i'll get the same error. I also tried the script in an other PowerShell console i found on the interwebs, and there i get an entirely different error :
PS C:\temp.
ps1
The following exception occurred while retrieving member "Validate": "Could not
load file or assembly 'MyOwn.Entit
ies, Version=1.0.0.0, Culture=neutral, PublicKeyToken=fc80acb30cba5fab' or one
of its dependencies. The system cannot find the file specified."
At D:\Projects\temp.ps1:145 char:30
+ $MyProblemCollection.Validate <<<< ()
+ CategoryInfo : NotSpecified: (:) [], ExtendedTypeSystemExceptio
n
+ FullyQualifiedErrorId : CatchFromBaseGetMember
hmm nvm, it spontaneously worked! strange BUT i'm happy! =)
Your method is static, but you are trying to call it via an instance.
$MyProblemCollection = new-Object ComponentSubtypeMustMatchTemplate
Creates a new instance of ComponentSubtypeMustMatchTemplate.
$MyProblemCollection.Validate()
Calls the instance method named Validate on the object referenced by $MyProblemCollection. However, since there is no instance method named Validate, the call fails.
You can either make the methods non-static (which you would do if they were going to keep state in the object), or you can use a different powershell syntax used to call a static method:
$result = [ComponentSubtypeMustMatchTemplate]::Validate()
Remember ( if you keep these static ) that setting a static property means anything querying that property in the same process will get that value. If you are holding state, then I would advise removing the static from each declaration.