Replacing Func with delegates C#
Asked Answered
M

4

3

We are trying to move an application built in .NET 3.5 to 2.0 (reason to let our exe run on older machines with XP etc. which does not have 3.5)

While doing so everything is now stuck on one major problem Replacing Func with an old fashioned delegate (As Func is not available on 2.0). The code to be replaced is something like this.

private Func<object, string> someName1;
private static Func<object, string> someName2;

internal Func<object, string> someProperty
{
      get { return someName1?? (someName1= someName2); }
      set { someName1= value; }
}

Can some body please help me create 'someProperty' the way it is only by using delegates. Thanks in advance.

Muttonhead answered 7/6, 2013 at 10:44 Comment(0)
G
12
public delegate void Action();
public delegate void Action<T>(T t);
public delegate void Action<T, U>(T t, U u);
public delegate void Action<T, U, V>(T t, U u, V v);

public delegate TResult Func<TResult>();
public delegate TResult Func<T, TResult>(T t);
public delegate TResult Func<T, U, TResult>(T t, U u);
public delegate TResult Func<T, U, V, TResult>(T t, U u, V v);
public delegate TResult Func<T, U, V, W, TResult>(T t, U u, V v, W w);
Gareth answered 7/6, 2013 at 10:48 Comment(1)
Note that .NET 2.0 did have the Action<T> delegate with exactly one generic type argument (your second line). All the others were "missing", so they are OK to introduce in user code.Helicoid
W
0

Specifically:

public delegate string SomeDelegate(object someObject);

private SomeDelegate someName1;
private static SomeDelegate someName2;

internal SomeDelegate someProperty
{
      get { return someName1?? (someName1= someName2); }
      set { someName1= value; }
}
Whitneywhitson answered 7/6, 2013 at 10:48 Comment(0)
T
0

I believe you can use LinqBridge which defines the Action and Func delegates from .Net 3.5

Tullusus answered 7/6, 2013 at 10:50 Comment(0)
P
0

.Net2.0 Code with delegate looks like below:

    private delegate void SomeName(object arg1, string arg2);
    SomeName someName1, someName2;

    internal SomeName SomeProperty
    {
        get
        {
            return someName1 ?? (someName1 = someName2);
        }
        set
        {
            someName1 = value;
        }
    }
Penultimate answered 7/6, 2013 at 11:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.