© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
19 replies
hellounlimited

interface instead of mapping one by one

hi all, im trying to use an interface to initialize common properties, for example INote that contains all common properties, and multiple classes will inherit from this INote, im trying to achieve something like : initialize once and can be used for all objects that inherit this INote interface

previously i was doing one by one mapping for each objects that have common properties and found out there are lots of duplication (some resource say better to do manual mapping that use a library like automapper)

so i try to do it using interface, but somehow even if a class inherit this INote i still can't do it like this :
Memo resource = QueryVariant(new NoteItem(), "memo");
LongNote longNote = QueryVariant(new NoteItem(), "longNote");

did i miss anything here?

public interface INote{
  public long NodeId { get; set; }
  public string NoteName { get; set; }
}

public class LongNote : INote {
  
  public long NodeId { get; set; }
  public string NoteName { get; set; }

  public string barcodeID {get;set;}
  public string profileURL {get;set;}
}
 
public class Memo: INote {
  
  public long NodeId { get; set; }
  public string NoteName { get; set; }

  public string memoURL {get;set;}
  public string memoUniqueID {get;set;}
}

Memo resource = QueryVariant(new NoteItem(), "memo"); //doesnt work

private async Task<INote> QueryVariant(NoteItem item, string type) {
  INote note = type switch {
    "memo" => new Memo { /*initialize properties here*/ };
    // replicate for the other types
    _ => // throw some exception here
  };
  // initialize common properties in note here, i.e:
  note.id = item.Id.ToString():
  note.noteName = item.name;
  // etc
  return note;
}
public interface INote{
  public long NodeId { get; set; }
  public string NoteName { get; set; }
}

public class LongNote : INote {
  
  public long NodeId { get; set; }
  public string NoteName { get; set; }

  public string barcodeID {get;set;}
  public string profileURL {get;set;}
}
 
public class Memo: INote {
  
  public long NodeId { get; set; }
  public string NoteName { get; set; }

  public string memoURL {get;set;}
  public string memoUniqueID {get;set;}
}

Memo resource = QueryVariant(new NoteItem(), "memo"); //doesnt work

private async Task<INote> QueryVariant(NoteItem item, string type) {
  INote note = type switch {
    "memo" => new Memo { /*initialize properties here*/ };
    // replicate for the other types
    _ => // throw some exception here
  };
  // initialize common properties in note here, i.e:
  note.id = item.Id.ToString():
  note.noteName = item.name;
  // etc
  return note;
}
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

❔ Importing functions by RVA instead of Name
C#CC# / help
4y ago
Getting constant address instead of ephemeral one
C#CC# / help
6mo ago
✅ how can i let an interface implement a function of a super-interface, instead of shadowing it?
C#CC# / help
2y ago