C
C#4w ago
RV

✅ Callable Class

using System.Dynamic;

Run run = new Run();

run["Hello World"]();
Console.WriteLine(run[Run.Expression.SUM, 1, 2, 4, 2, 4]());

dynamic run2 = new Run();
run2();

class Run : DynamicObject
{
public enum Expression
{
SUM,
AVERAGE,
}
public override bool TryInvoke(InvokeBinder binder, object?[]? args, out object? result)
{
Console.WriteLine("Invoked");
result = null;
return true;
}

public Action this[string message] => () => Console.WriteLine(message);

public Func<double> this[Expression expression, params int[] _] => () =>
{
if (expression == Expression.SUM)
return _.Sum();
else
return _.Average();
};
}
using System.Dynamic;

Run run = new Run();

run["Hello World"]();
Console.WriteLine(run[Run.Expression.SUM, 1, 2, 4, 2, 4]());

dynamic run2 = new Run();
run2();

class Run : DynamicObject
{
public enum Expression
{
SUM,
AVERAGE,
}
public override bool TryInvoke(InvokeBinder binder, object?[]? args, out object? result)
{
Console.WriteLine("Invoked");
result = null;
return true;
}

public Action this[string message] => () => Console.WriteLine(message);

public Func<double> this[Expression expression, params int[] _] => () =>
{
if (expression == Expression.SUM)
return _.Sum();
else
return _.Average();
};
}
How to use Callable Class in C#? like
var d = Class1();
d();
var d = Class1();
d();
like in python __call__ or dart?
14 Replies
Unknown User
Unknown User4w ago
Message Not Public
Sign In & Join Server To View
RV
RV4w ago
Yeah, without dynamic object.
Unknown User
Unknown User4w ago
Message Not Public
Sign In & Join Server To View
Angius
Angius4w ago
For future reference, though, C# is neither Python nor Dart. It's generally best to use C# as it was intended, not trying to coerce it into how other languages work.
Unknown User
Unknown User4w ago
Message Not Public
Sign In & Join Server To View
RV
RV4w ago
i was talk about callable class, this code just sample.
Unknown User
Unknown User4w ago
Message Not Public
Sign In & Join Server To View
Angius
Angius4w ago
Why do you even need something like that?
Unknown User
Unknown User4w ago
Message Not Public
Sign In & Join Server To View
Angius
Angius4w ago
I've never seen callable classes in C# in the wild
Unknown User
Unknown User4w ago
Message Not Public
Sign In & Join Server To View
Angius
Angius4w ago
It just seems like a useless overcomplication of something otherwise super simple
var x = new Calc(1,2,3,4);

var result = x.Compute(Operation.Average);

enum Operation
{
Sum,
Average,
}

public class Calc(int[] operands)
{
int Compute(Operation op) => op switch {
Operation.Sum => operands.Sum(),
Operation.Average => operands.Average(),
_ => throw new Exception("C# enums suck ass")
};
}
var x = new Calc(1,2,3,4);

var result = x.Compute(Operation.Average);

enum Operation
{
Sum,
Average,
}

public class Calc(int[] operands)
{
int Compute(Operation op) => op switch {
Operation.Sum => operands.Sum(),
Operation.Average => operands.Average(),
_ => throw new Exception("C# enums suck ass")
};
}
If you want multiple operations as an enum
Unknown User
Unknown User4w ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX4w ago
If you have no further questions, please use /close to mark the forum thread as answered
Want results from more Discord servers?
Add your server