Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1080637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:03:47+00:00 2026-05-16T22:03:47+00:00

I’ve made a c# class that I’m trying to run in PowerShell with add-Type.

  • 0

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! =)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T22:03:47+00:00Added an answer on May 16, 2026 at 10:03 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.