There is a need to set a text mask for two components of different types. Each of them supports this feature. I want to solve this with one method. I have never used it before, I think it's time to explore universal methods. I write this:

public static void SetDocNumMask<T>(int id_doctype, ref T ctrl) { ctrl.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.Simple; ctrl.Mask.EditMask = new string('0', 9); } 

Studio swears at the word Mask

This is a list of how it can be found. or an assembly assembly?)

I call the method in two places

 Mask.SetDocNumMask<DevExpress.XtraEditors.ComboBoxEdit>(doc_‌​type, ref cbDocNum); Mask.SetDocNumMask< DevExpress.XtraEditors.Repository.RepositoryItemTextEdit>(id‌​_doctype, ref repForDocNum); 

What am I doing wrong?

  • for what components? - Grundy
  • 3
    What am I doing wrong? - at the moment type T can be any. Even to those who do not have the Mask property, so the compiler quite rightly says that it is impossible to do this. - Grundy
  • one
    You describe the generic method and do not specify any restrictions in the style of the base class or interface. Therefore, the studio swears as the most common type according to c # is an object in which there is naturally no Mask - AvtPhenix
  • Mask.SetDocNumMask <DevExpress.XtraEditors.ComboBoxEdit> (doc_type, ref cbDocNum); Mask.SetDocNumMask <DevExpress.XtraEditors.Repository.RepositoryItemTextEdit> (id_doctype, ref repForDocNum); - Antykus
  • one
    @Antykus, judging by their documentation - this property doesn’t have this control - Grundy

1 answer 1

Consider that in your case T is object . No one guarantees that the ctrl will be the property Mask . In order to guarantee this, it is necessary to specify the restriction - the base type or the interface that this property contains:

 interface IMasked { MaskType Mask { get; } } public static void SetDocNumMask<T>(int id_doctype, ref T ctrl) where T: IMasked // <<---- { ctrl.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.Simple; ctrl.Mask.EditMask = new string('0', 9); } 

ZY If a type can be directly generally, in general, any and no base type / interface with a Mask does not inherit it, then you can declare ctrl as dynamic . The problem will be poor readability and the lack of a static type checking by the compiler.

  • if ctrl is declared as dynamic, then generic can be removed, since there will no longer be any sense in it - Grundy
  • @Grundy You are right, I meant dynamic, as a collective farm alternative to generics) - free_ze
  • 2
    * "The problem will be poor readability and the lack of a static type checking by the compiler." - and do not forget about the speed of execution! - Pavel Mayorov
  • @free_ze I'm sorry, stupid, I understood the point. but I can't figure out how to make it possible to call a method indicating both types: DevExpress.XtraEditors.Repository.RepositoryItemTextEdit and DevExpress.XtraEditors.ComboBoxEdit - Antykus
  • one
    @Antykus if they have a common parent (base class or interface) that declares the Mask property, then it must be specified in the restriction (instead of IMasked in my example). If not, and these types cannot be inherited and wrapped (because the struct, judging by the fact that you specified ref), then you have to remove the generics and instead of the type for ctrl, specify dynamic. - free_ze