© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
9 replies
Indeed

❔ Class with parameter as a generic parameter

Hi!
I'm trying to write a navigation system in wpf

My setup rn using default constructor is

public class ViewModelBase : INotifyPropertyChanged {
    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName) {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    public virtual void Dispose() { }
}
public class ViewModelBase : INotifyPropertyChanged {
    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName) {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    public virtual void Dispose() { }
}


public class NavigationService<TViewModel> where TViewModel : ViewModelBase {
    private readonly NavigationStore _navigationStore;
    private readonly Func<TViewModel> _createViewModel;

    public NavigationService(NavigationStore navigationStore, Func<TViewModel> createViewModel) {
        _navigationStore = navigationStore;
        _createViewModel = createViewModel;
    }

    public void Navigate() {
        _navigationStore.CurrentViewModel = _createViewModel();
    }
}
public class NavigationService<TViewModel> where TViewModel : ViewModelBase {
    private readonly NavigationStore _navigationStore;
    private readonly Func<TViewModel> _createViewModel;

    public NavigationService(NavigationStore navigationStore, Func<TViewModel> createViewModel) {
        _navigationStore = navigationStore;
        _createViewModel = createViewModel;
    }

    public void Navigate() {
        _navigationStore.CurrentViewModel = _createViewModel();
    }
}


however i want to change it so each ViewModelBase has a public custom constructor like
TViewModel.NavigateTo()
TViewModel.NavigateTo()
that returns its instance to be able to define some common logic between navigables

I've tried to do
public interface Navigable<TViewModel> where TViewModel : ViewModelBase {
    TViewModel NavigatedTo();
}
public interface Navigable<TViewModel> where TViewModel : ViewModelBase {
    TViewModel NavigatedTo();
}


public class NavigationService<Navigable<TViewModel>> where TViewModel : ViewModelBase {
public class NavigationService<Navigable<TViewModel>> where TViewModel : ViewModelBase {

but it breaks
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

Use derived classes as generic parameter
C#CC# / help
4y ago
❔ ✅ class parameter
C#CC# / help
4y ago
❔ Pass variable type as class parameter
C#CC# / help
3y ago
Struggling with generic class... design?
C#CC# / help
2y ago