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
  • 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-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

Ask A Question

Stats

  • Questions 499k
  • Answers 500k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This is not pretty but it works: rm -R $(ls… May 16, 2026 at 12:45 pm
  • Editorial Team
    Editorial Team added an answer Yes. Override the base1 and base2 methods in Derived to… May 16, 2026 at 12:45 pm
  • Editorial Team
    Editorial Team added an answer No, you can't. Unfortunately, UIEvent doesn't expose any public way… May 16, 2026 at 12:45 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have a form submit button that has asp.net validators hooked up to it.
I have a page on my ASP.NET site that has a Repeater control to
I have a c# .NET assembly that gets called from a C++ COM client.
I have a Page (form) that has many UserControls on it, and I'm trying
I have an application that was written in vanilla ASP.NET that I would like
I think I need some help understanding how static objects persist in an ASP.Net
I have a static method like this public static string DoSomethingToString(string UntrustedString) { //parse
We have a piece of code which saves a .Net System.Drawing.Bitmap to file. The
I have an application written in VB.NET ( NOT asp.net, it is a Windows
I am using Excel interop in my VB.NET program. My problem is that I

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.