C
C#2y ago
kitsune vi

❔ Use `this` inside method decorator

Tried to follow https://stackoverflow.com/a/2966758
[AttributeUsage(AttributeTargets.Method)]
private class GUIEffect : Attribute
{
public void PerformCall(Action action)
{
action.Invoke();
//action.somehowGetThis.UpdateGUI()
}
}
[AttributeUsage(AttributeTargets.Method)]
private class GUIEffect : Attribute
{
public void PerformCall(Action action)
{
action.Invoke();
//action.somehowGetThis.UpdateGUI()
}
}
Can't figure out how to access this using method decorator
Stack Overflow
Can I use the decorator pattern to wrap a method body?
I have a bunch of methods with varying signatures. These methods interact with a fragile data connection, so we often use a helper class to perform retries/reconnects, etc. Like so: MyHelper.
23 Replies
kitsune vi
kitsune viOP2y ago
side note I was like "how hard can it be I will just factor out the simple side effect code into a decorator and have it up front" oh how naive @nekodjin ^u^
sampersand
sampersand2y ago
lol
kitsune vi
kitsune viOP2y ago
oh damn you too huh yeah I actually thought decorators of all thing would be a small cost abstraction to make my code a little prettier I'm f*cking stupid sigh but alas, I still want my pretty [Effect] boxes so, here we are
nekodjin
nekodjin2y ago
C# doesn't have decorators
Angius
Angius2y ago
Attributes are just metadata For some other code to find and do something with They're not like C++ macros or anything of the sort
kitsune vi
kitsune viOP2y ago
but you can sandwich the original method in extra stuffs right?
nekodjin
nekodjin2y ago
no.. if so it'd be a decorator :p
Angius
Angius2y ago
Only in a very roundabout way. The attribute can contain a Pre and Post methods, then you can get a method with this attribute using reflections, and execute Pre() then ActualMethod() then Post() Attributes themselves don't ever execute Something has to execute them
nekodjin
nekodjin2y ago
you could do some build-time codegen shit
Angius
Angius2y ago
You can, of course, do something like
int Wrapper(Func<int, int, int> func)
{
// pre code
var res = func(2, 2);
// post code
return res;
}
int Wrapper(Func<int, int, int> func)
{
// pre code
var res = func(2, 2);
// post code
return res;
}
and call it with
Wrapper((a, b) => a + b)
Wrapper((a, b) => a + b)
kitsune vi
kitsune viOP2y ago
mmm I did an inefficient but it makes my brain happy
nekodjin
nekodjin2y ago
the long and short of it, vi, is give up the decorator idea if i know anything about what you might be trying to use it for lol
kitsune vi
kitsune viOP2y ago
[AttributeUsage(AttributeTargets.Method)]
private class GUIEffect : Attribute
{
public void PerformCall(Action action)
{
action.Invoke();
GameLogicSingletonScript.Instance.UpdateGUI();
}
}
public static GameLogicSingletonScript Instance
{
get
{
return GameObject
.FindGameObjectWithTag("GameController")
.gameObject
.GetComponent<GameLogicSingletonScript>();
}
}
[AttributeUsage(AttributeTargets.Method)]
private class GUIEffect : Attribute
{
public void PerformCall(Action action)
{
action.Invoke();
GameLogicSingletonScript.Instance.UpdateGUI();
}
}
public static GameLogicSingletonScript Instance
{
get
{
return GameObject
.FindGameObjectWithTag("GameController")
.gameObject
.GetComponent<GameLogicSingletonScript>();
}
}
>.<
kitsune vi
kitsune viOP2y ago
yeah, sigh no actually interesting decorators
Angius
Angius2y ago
https://github.com/Fody/MethodDecoratorwhen shenanigans with Fody are an option if you really want decorators
nekodjin
nekodjin2y ago
oh no.. don't encourage her 😭 she'll end up trying to make monads with a do notation decorator or smth
kitsune vi
kitsune viOP2y ago
o.o OwO algebraic effects in C#? >.>
nekodjin
nekodjin2y ago
aaaaaaaaaaaaa
kitsune vi
kitsune viOP2y ago
listen I autismo
nekodjin
nekodjin2y ago
sigh
kitsune vi
kitsune viOP2y ago
FP go brr
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server