C
C#17mo ago
SWEETPONY

✅ How to call base implementation in abstract class?

I have following abstract class:
[AttributeUsage( AttributeTargets.Property | AttributeTargets.Field )]
public abstract class UIControlAttribute
: Attribute
{
[JsonProperty(PropertyName = "group")]
public string Group { get; set; }

public virtual void FillSchema(
UISchemaItem schemaItem,
PropertyInfo property,
CultureInfo culture)
{
if ( Group != null )
schemaItem.ParsecMetadata.Group = Group;
}
}
[AttributeUsage( AttributeTargets.Property | AttributeTargets.Field )]
public abstract class UIControlAttribute
: Attribute
{
[JsonProperty(PropertyName = "group")]
public string Group { get; set; }

public virtual void FillSchema(
UISchemaItem schemaItem,
PropertyInfo property,
CultureInfo culture)
{
if ( Group != null )
schemaItem.ParsecMetadata.Group = Group;
}
}
Usage:
public class UIControlAesKeyAttribute
: UIControlAttribute
{
public override void FillSchema(
UISchemaItem schemaItem,
PropertyInfo property,
CultureInfo culture)
{
schemaItem.Type = "string";

schemaItem.ParsecMetadata ??= new();
schemaItem.ParsecMetadata.UIControlInputType = UIControlInputType.AesKey;
}
}
public class UIControlAesKeyAttribute
: UIControlAttribute
{
public override void FillSchema(
UISchemaItem schemaItem,
PropertyInfo property,
CultureInfo culture)
{
schemaItem.Type = "string";

schemaItem.ParsecMetadata ??= new();
schemaItem.ParsecMetadata.UIControlInputType = UIControlInputType.AesKey;
}
}
The problem: I want to do schemaItem.ParsecMetadata.Group = Group; in all attributes that inherit UIControlAttribute I know that I can call base.FillSchema( schemaItem, property, culture ); but maybe there is better way to do the same?
32 Replies
ero
ero17mo ago
unrelated but what language are you using for those code blocks?
sneezey
sneezey17mo ago
not sure how right I am, but: You could make a new method that the new functionality overrides, but is a protected keyword method? then the FillSchema can call the new method, then you don't need to remember the base.FillSchema(...) ...? language? you know what server we're in right? 😹
SWEETPONY
SWEETPONYOP17mo ago
cpp
ero
ero17mo ago
yeah you're gonna wanna use cs ;)
sneezey
sneezey17mo ago
ohh, i see what you mean 😂 for the markdown language support ```csharp <code> ```
SWEETPONY
SWEETPONYOP17mo ago
what about this guys?
[AttributeUsage( AttributeTargets.Property | AttributeTargets.Field )]
public abstract class UIControlAttribute
: Attribute
{
[JsonProperty(PropertyName = "group")]
public string Group { get; set; }

public void FillSchema(
UISchemaItem schemaItem,
PropertyInfo property,
CultureInfo culture)
{
FillSchemaDetails(
schemaItem,
property,
culture);

if (Group != null)
schemaItem.ParsecMetadata.Group = Group;
}

public abstract void FillSchemaDetails(
UISchemaItem schemaItem,
PropertyInfo property,
CultureInfo culture);
}
[AttributeUsage( AttributeTargets.Property | AttributeTargets.Field )]
public abstract class UIControlAttribute
: Attribute
{
[JsonProperty(PropertyName = "group")]
public string Group { get; set; }

public void FillSchema(
UISchemaItem schemaItem,
PropertyInfo property,
CultureInfo culture)
{
FillSchemaDetails(
schemaItem,
property,
culture);

if (Group != null)
schemaItem.ParsecMetadata.Group = Group;
}

public abstract void FillSchemaDetails(
UISchemaItem schemaItem,
PropertyInfo property,
CultureInfo culture);
}
and then I override FillSchemaDetails not FillSchema but call FillSchema
sibber
sibber17mo ago
$code
MODiX
MODiX17mo ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat If your code is too long, post it to: https://paste.mod.gg/
sneezey
sneezey17mo ago
I think you can make FillSchemaDetails protected, then it forces usage on the FillSchema and wont be tempted to use FillSchemaDetails? happy to be told otherwise 😆
ero
ero17mo ago
calling the base ctor is the correct option here
sibber
sibber17mo ago
thats what i was going to suggest
SWEETPONY
SWEETPONYOP17mo ago
I don't understand why we don't have public protected abstract
ero
ero17mo ago
we do?
sneezey
sneezey17mo ago
you cant have public + protected at the same time 😅
sibber
sibber17mo ago
.net uses that approach actually
ero
ero17mo ago
yeah they corrected it lol
SWEETPONY
SWEETPONYOP17mo ago
ah I'm sorry
sibber
sibber17mo ago
e.g. for collections, they dont let you override .Add, instead you override .AddItem
sneezey
sneezey17mo ago
^ this i was giving another option, but doing base. is the right thing
sibber
sibber17mo ago
what do you expect that to do
SWEETPONY
SWEETPONYOP17mo ago
I wrote smth wrong but I just need to do following: 1. can override 2. don't let to call
ero
ero17mo ago
this seems like a bit of a weird use of an attribute
sibber
sibber17mo ago
dont let who call it? can override => virtual have to override => abstract
SWEETPONY
SWEETPONYOP17mo ago
let's see:
var controlAttribute = property
.GetCustomAttribute<UIControlAttribute>();

controlAttribute.FillSchema( schemaItem, property, culture );
var controlAttribute = property
.GetCustomAttribute<UIControlAttribute>();

controlAttribute.FillSchema( schemaItem, property, culture );
using FillSchema is ok but I can call FillSchemaDetails and it is not ok
ero
ero17mo ago
. also can you stop using ```cpp lol not that it matters actually
SWEETPONY
SWEETPONYOP17mo ago
protected abstract?
ero
ero17mo ago
mhm
sibber
sibber17mo ago
dont make it public then
ero
ero17mo ago
didn't wanna make it sound like it's that big a deal
sneezey
sneezey17mo ago
protected abstract void FillSchemaDetails(
UISchemaItem schemaItem,
PropertyInfo property,
CultureInfo culture);
protected abstract void FillSchemaDetails(
UISchemaItem schemaItem,
PropertyInfo property,
CultureInfo culture);
like that - the first word from public to protected
SWEETPONY
SWEETPONYOP17mo ago
ahh okay, I understand now
protected abstract void FillSchemaDetails(..)

public class UIControlCatalogAttribute
: UIControlAttribute
{
protected override void FillSchemaDetails(..)
}
protected abstract void FillSchemaDetails(..)

public class UIControlCatalogAttribute
: UIControlAttribute
{
protected override void FillSchemaDetails(..)
}
yeah, thanks for helping
sneezey
sneezey17mo ago
🙌
Want results from more Discord servers?
Add your server