null check raising errors

i come from python and my first interpretation was that they can work like decorators, but that's clearly not the case. my question is how do i modify class data with an attribute? like how in python you can decorate a class like this:
def change(name, value):
def wrapper(cls):
setattr(cls, name, value)
return cls

return wrapper

# ...

@change("num", 4)
class MyClass:
def __init__(self):
self.num = 10

mc = MyClass()
print(mc.num) # prints 4 and not 10
def change(name, value):
def wrapper(cls):
setattr(cls, name, value)
return cls

return wrapper

# ...

@change("num", 4)
class MyClass:
def __init__(self):
self.num = 10

mc = MyClass()
print(mc.num) # prints 4 and not 10
how would i do this in C#?
53 Replies
Angius
Angius2mo ago
Attributes do nothing They are just markers They can be used by reflections or source generators, and those can provide additional functionality Is there anything in particular you need to do, or is it just a theoretical question? Or you can go offline, sure, that's also fine
it’s raining outside
oh sorry, i shut my laptop to go make breakfast. im back now i want to set a docstring on a command class that says how the user explains the command to work
Angius
Angius2mo ago
No need for attributes there Just use documentation comments
/// <summary>
/// This method does foobar
/// </summary>
public void FooBar()
{
//...
}
/// <summary>
/// This method does foobar
/// </summary>
public void FooBar()
{
//...
}
Just type /// before whatever you want to document, and your IDE will automatically generate a doc string, with all the parameters, return, and so on
it’s raining outside
i was making my own CLI so code comments wouldnt work
Angius
Angius2mo ago
You'll just have to fill it in Ah, well, attributes in this case, sure Probably used with reflections Or with source generators
it’s raining outside
how would i do those then what are source generators
Angius
Angius2mo ago
Both are fairly advanced topics Source generators more so than reflections Source generators let you... generate source code. They allow you to write code that inspects your existing code and generates new code As for how, Microsoft's docs are fairly good, at least on the topic of reflections, so I'd start there
it’s raining outside
ah ok will definitely look at it by the way, do you know how to do this?
it’s raining outside
basically i have a property called CalledCommands that gets all the subclasses of the Command class but of course, it's just a generic Type type
Angius
Angius2mo ago
You want an instance of Command?
it’s raining outside
not an instance of Command, all the subclasses of Command it works fine and gets the correct names, but i dont get how i convert from Type to Command
Angius
Angius2mo ago
Activator.CreateInstance() method will be useful It lets you create an instance from a Type
it’s raining outside
ohh i dont have an empty constructor tho
Angius
Angius2mo ago
It lets you pass parameters too Just look up the docs
it’s raining outside
how does that help? sorry, i really just dont get it how does creating an instance work? do i not already have the instances? oh wait yep, still dont get it
Angius
Angius2mo ago
If I may suggest some other, simpler project for learning purposes?
it’s raining outside
uh sure
Angius
Angius2mo ago
That said, to do anything, you need an instance of a given class, an object
it’s raining outside
ah ok makes sense
Angius
Angius2mo ago
A Type instance that stores type info of Command is not an instance of Command If you want to list all subclasses of Command based on the Type you'll need to use reflections to get them Then, from what I gather from the code, you'll want to instantiate each of them, and store them in a list?
Angius
Angius2mo ago
Ah, wait, scratch that, you are getting all subclasses of command already So, yeah, only instantiation is left
it’s raining outside
the help command gathers all the created commands and returns the docstring the user applies to them
>>> help echo
Help on command 'Builtins.EchoCommand':
---------------------------------------
hi hi hi 3 hio worlding sequence of characters, joined by a space.r of times
---------------------------------------
>>> help echo
Help on command 'Builtins.EchoCommand':
---------------------------------------
hi hi hi 3 hio worlding sequence of characters, joined by a space.r of times
---------------------------------------
for some reason, this is the console output
it’s raining outside
when printing it in dotnetfiddle.net, it works fine
Angius
Angius2mo ago
Uh No idea how would you ever get this output lmao
it’s raining outside
(sorry for light mode users)
No description
it’s raining outside
new bug found
Angius
Angius2mo ago
Do you use any async code at all? That's about the only thing that comes to mind
it’s raining outside
nope fully sync
Angius
Angius2mo ago
Something being printed, while something else moves the cursor position I'm at a loss, then I'd probably just opt for an existing CLI library lol
it’s raining outside
lemme check my WriteLine funcs boring
Angius
Angius2mo ago
See if any of your code sets cursor position Comment those bits out and try again Also, make sure everything actually is fully sync, and not some shenanigans like calling an async method in a synchronous context
it’s raining outside
nope im lost as well https://mystb.in/112d96a1efdbc92179 heres my code btw the builtins section
Angius
Angius2mo ago
Post the whole repo, maybe the error is somewhere else Everything here looks just fine I'll take a look tomorrow, gotta go to sleep now
Angius
Angius2mo ago
I guess you could also try stepping through the code with the debugger, idk
it’s raining outside
found the error i did .Replace("\n", "").Replace("\n\n", "\n") to turn two new lines into one and one new line into nothing but i think i fucked something up there
Pobiega
Pobiega2mo ago
regarding using attributes for command documentation, you can look at one of the many other CLI frameworks that already do this. https://github.com/Tyrrrz/CliFx comes to mind as my personal favorite
it’s raining outside
back from the gym oh that looks pretty cool. i'll do my best to adopt a few concepts from there
it’s raining outside
i get this error even though (i think) i've correctly overloaded ==. heres my code - https://mystb.in/2c965b9e95b80075f2 - and let me know what i did wrong
No description
LPeter1997
LPeter19972mo ago
Can you show the definition of Command, is that defined by you?
it’s raining outside
its in the pastebin
LPeter1997
LPeter19972mo ago
Yeah so you define an equality with string and command null could be either
LPeter1997
LPeter19972mo ago
Hence the ambuiguity error, doesn't know which == to invoke here You can use ReferenceEquals(command, null) If you wanna circumvent this (Even better if you remove the string versions of == IMO)
it’s raining outside
well i want to be able to compare a string with an argument so if the argument name is the same as the string or the string is the alias of an argument wait should i just add a cast?
LPeter1997
LPeter19972mo ago
What's wrong with typing cmd.Name == ... You can have null as Command or null as string yes That will disambiguate it But ReferenceEquals works too
it’s raining outside
i meant explicitly comparing with a string, eg.
Argument arg = new("arg1", "-a")

// Then being able to use this:
arg == "arg1"
Argument arg = new("arg1", "-a")

// Then being able to use this:
arg == "arg1"
whats that
LPeter1997
LPeter19972mo ago
ReferenceEquals literally checks, if the two references are the same. Similar to pointer equality in C or something, checks if the two things are literally the same instance, disregarding any other equality defined on the types.
Want results from more Discord servers?
Add your server