简体   繁体   中英

How to access properties of generic base class's parameter in C#?

I have a common class PopupDialog which has properties such as bool IsBackDismissEnabled . This is used to denote whether pressing back button will dismiss the dialog.

I am using Xamarin.CommunityToolkit Popup for showing a popup dialog.

Here's my PopupDialog.cs

public class PopupDialog
{
    public bool IsBackDismissEnabled { get; set; }
}

Here's my Dialog Implementation

using Xamarin.CommunityToolkit.UI.Views;

[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class ForgotPasswordDialog : Popup<PopupDialog>
{
    public ForgotPasswordDialog()
    {
        InitializeComponent();

        // I want to access IsBackDismissEnabled here
        // something like base.IsBackDismissEnabled = true;
    }
}

I want to access PopupDialog 's IsBackDismissEnabled from the derived class of Popup<PopupDialog> how can it be done?

In short, I have a class which is specified as a parameter to a generic class. And that generic class is derived in a class from which I want to access properties of previous class that is specified as a parameter to the generic class.

I've never seen anyone attempt to do what you are attempting. I'm not even sure it makes logical sense. Please explain what you are trying to do: Why do you want PopupDialog to be a generic parameter ?

Consider making it a parameter on Popup 's constructor, and save it in a property or field:

public class Popup
{
    public Popup(ISomeInterface myParam)
    {
        this.MyParam = myParam;
    }

    public ISomeInterface MyParam;
}

public class ForgotPasswordDialog : Popup
{
    public ForgotPasswordDialog() : base(new PopupDialog())
    {
    }

    void SomeMethod()
    {
        // Access MyParam
        ... this.MyParam.IsBackDismissEnabled ...
    }
}

public interface ISomeInterface
{
    bool IsBackDismissEnabled { get; set; }
}

public class PopupDialog : ISomeInterface
{
    public bool IsBackDismissEnabled { get; set; }
    ...
}

/// Usage
var myVariable = new ForgotPasswordDialog();
... myVariable.MyParam.IsBackDismissEnabled ...

To be useful, you'll want to specify an interface or base class that MyParam has. Here I show ISomeInterface . This might instead be some base class of PopupDialog .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM