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