C
C#2y 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
ero2y ago
unrelated but what language are you using for those code blocks?
sneezey
sneezey2y 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
SWEETPONYOP2y ago
cpp
ero
ero2y ago
yeah you're gonna wanna use cs ;)
sneezey
sneezey2y ago
ohh, i see what you mean 😂 for the markdown language support ```csharp <code> ```
SWEETPONY
SWEETPONYOP2y 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
sibber2y ago
$code
MODiX
MODiX2y 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
sneezey2y 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
ero2y ago
calling the base ctor is the correct option here
sibber
sibber2y ago
thats what i was going to suggest
SWEETPONY
SWEETPONYOP2y ago
I don't understand why we don't have public protected abstract
ero
ero2y ago
we do?
sneezey
sneezey2y ago
you cant have public + protected at the same time 😅
sibber
sibber2y ago
.net uses that approach actually
ero
ero2y ago
yeah they corrected it lol
SWEETPONY
SWEETPONYOP2y ago
ah I'm sorry
sibber
sibber2y ago
e.g. for collections, they dont let you override .Add, instead you override .AddItem
sneezey
sneezey2y ago
^ this i was giving another option, but doing base. is the right thing
sibber
sibber2y ago
what do you expect that to do
SWEETPONY
SWEETPONYOP2y ago
I wrote smth wrong but I just need to do following: 1. can override 2. don't let to call
ero
ero2y ago
this seems like a bit of a weird use of an attribute
sibber
sibber2y ago
dont let who call it? can override => virtual have to override => abstract
SWEETPONY
SWEETPONYOP2y 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
ero2y ago
. also can you stop using ```cpp lol not that it matters actually
SWEETPONY
SWEETPONYOP2y ago
protected abstract?
ero
ero2y ago
mhm
sibber
sibber2y ago
dont make it public then
ero
ero2y ago
didn't wanna make it sound like it's that big a deal
sneezey
sneezey2y 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
SWEETPONYOP2y 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
sneezey2y ago
🙌

Did you find this page helpful?