Is it possible to convert type to setter method? When yes, how?

Suppose I wanted to reinstall the command line with flags and so on. Flags are of type bool, but the command line is a string of type "/ activeFlag". Is there a way to program a setter in C # that accepts a bool, but getter returns a string?

as

private string activeFlag {
 get { return activeFlag; }
 set {
    // the value here should be the bool
    activeFlag = value ? " /activeFlag" : "";
 }
}
+3
source share
4 answers

You need a different setter.

private string activeFlag { 
 get { return _activeFlag; } 
}
private bool activeFlagByBool { 
 set { 
    // the value here should be the bool 
    _activeFlag = value ? " /activeFlag" : ""; 
 } 
} 
0
source

There is no way to have a property with different data types for its setter and getter.

What you can do is something like this:

private bool IsActiveFlagSet { get { return ActiveFlag == " /activeFlag"; } }
private string ActiveFlag { get; set; }
+9
source

, TypeConverter , TypeDescriptor. . , , DataGridView, PropertyGrid .

; - . :

[CommandLine("/activeFlag")]
public bool IsActive {get;set;}

?

0

, # , .

0

Source: https://habr.com/ru/post/1719608/


All Articles