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 1013071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:02:34+00:00 2026-05-16T10:02:34+00:00

We have a .Net object that has a static delegate (a data access strategy)

  • 0

We have a .Net object that has a static delegate (a data access strategy) that has to be set before the object can be instantiated. The delegate can also be passed in through one of the constructor overloads.

I have another object that has the delegate that I need to set, but I can’t figure out in PowerShell how to do so. Does anyone know how to set a static delegate property or pass a delegate into a constructor in PowerShell?

I want to do something similar to this:

[DALObject]$db = New-Object DALObject;
[UnitOfWork]::Strategy = $db::CreateContext;

or

[DALObject]$db = New-Object DALObject;
[UnitOfWork]$uow = New-Object UnitOfWork($db::CreateConext);

Just for reference I am trying to reproduce the comparable C# code:

DalObject db = new DALObject();
UnitOfWork.Strategy = db.CreateContext;

or

UnitOfWork uow = new UnitOfWork(db.CreateContext);

Edit: To clarify Strategy is a static delegate

Public static Func<DataContext> Strategy 

and CreateContext is an instance method that I want to set to that delegate, either by setting the static Property or by passing in the method to the constructor.

Another option would be if there is there a way in PowerShell to create an anonymous delegate like this in C#:

 UnitOfWork.Strategy = delegate()
            {
                var ctx = db.CreateContext();
                return ctx;
            };
  • 1 1 Answer
  • 1 View
  • 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-16T10:02:35+00:00Added an answer on May 16, 2026 at 10:02 am

    You can’t access static members via an instance object (assuming here that CreateContext is a static property although it is named like a method). Try this:

    [DALObject]$db = New-Object DALObject; 
    [UnitOfWork]::Strategy = [DALObject]::CreateContext
    

    or if it is a method

    [DALObject]$db = New-Object DALObject; 
    [UnitOfWork]::Strategy = [DALObject]::CreateContext()
    

    OTOH if CreateContext is an instance member then change the [DALObject]::CreateContext to $db.CreateContext with or without parens depending on whether it is a method or not.

    Update: In the case of assigning a scriptblock to a generic delegate, there is no built-in way to make this work AFAIK. Check out this blog post for a potential work-around.

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

Sidebar

Related Questions

An interesting question arose today. Let's say I have a .NET object that implements
I have a Javascript object that basically represents a Row in an .NET GridView.
I have a COM object that I'm trying to use from C++ (not .NET),
I have an ASP.NET WebService that returns an object of List public class Students
i have an ASP.NET web service that returning a custom entity object (Staff): [WebMethod]
I have a few .Net projects that would benefit from using a document/object database
If I have an IEnumerable object in my .net code that contains a single
I have created an app in C# .Net 2.0 that uses the AxShockwaveFlash object
I have a .Net object (in C#) which has properties named event1, event2 and
In my .NET 4.0 project I've got an object that has public fields and

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.