В C# WPF мы можем использовать имя, как в < /p>
public class MyControl : Control
{
private static readonly DependencyProperty _myProperty =
DependencyProperty.Register(nameof(MyProperty), typeof(int), typeof(MyControl));
public int MyProperty
{
get => (int)GetValue(_myProperty);
set => SetValue(_myProperty, value);
}
}
< /code>
Но в F# это не работает < /p>
type MyControl() as this =
inherit Control()
static let _myProperty =
DependencyProperty.Register(nameof MyProperty, typeof, typeof) //The value or constructor 'Bars' is not defined.
member this.MyProperty
with get() = this.GetValue(_myProperty) :?> int
and set(value: int) = this.SetValue(_myProperty, value)
< /code>
Самый простой способ - использовать строку «myproperty», но это обладает печально известным эффектом, склонным к ошибке при переименовании свойства, если мы забываем изменить строку. < /p>
Есть ли способ сохранить название безопасности, которое C# дает в f#?open Microsoft.FSharp.Quotations
open Microsoft.FSharp.Quotations.Patterns
let getPropertyName (expr: Expr) =
match expr with
| Lambda(_, PropertyGet(_, propInfo, _)) -> propInfo.Name
| _ -> failwith "Invalid expression: Expected a property getter."
type MyControl() as this =
inherit Control()
static let _myProperty =
DependencyProperty.Register(getPropertyName c.MyProperty @>, typeof, typeof)
member this.MyProperty
with get() = this.GetValue(_myProperty) :?> int
and set(value: int) = this.SetValue(_myProperty, value)
< /code>
Но это явно громоздко. Есть лучший способ?
Подробнее здесь: https://stackoverflow.com/questions/793 ... imits-in-f
Название ограничений в F# ⇐ C#
Место общения программистов C#
1738211302
Anonymous
В C# WPF мы можем использовать имя, как в < /p>
public class MyControl : Control
{
private static readonly DependencyProperty _myProperty =
DependencyProperty.Register(nameof(MyProperty), typeof(int), typeof(MyControl));
public int MyProperty
{
get => (int)GetValue(_myProperty);
set => SetValue(_myProperty, value);
}
}
< /code>
Но в F# это не работает < /p>
type MyControl() as this =
inherit Control()
static let _myProperty =
DependencyProperty.Register(nameof MyProperty, typeof, typeof) //The value or constructor 'Bars' is not defined.
member this.MyProperty
with get() = this.GetValue(_myProperty) :?> int
and set(value: int) = this.SetValue(_myProperty, value)
< /code>
Самый простой способ - использовать строку «myproperty», но это обладает печально известным эффектом, склонным к ошибке при переименовании свойства, если мы забываем изменить строку. < /p>
Есть ли способ сохранить название безопасности, которое C# дает в f#?open Microsoft.FSharp.Quotations
open Microsoft.FSharp.Quotations.Patterns
let getPropertyName (expr: Expr) =
match expr with
| Lambda(_, PropertyGet(_, propInfo, _)) -> propInfo.Name
| _ -> failwith "Invalid expression: Expected a property getter."
type MyControl() as this =
inherit Control()
static let _myProperty =
DependencyProperty.Register(getPropertyName c.MyProperty @>, typeof, typeof)
member this.MyProperty
with get() = this.GetValue(_myProperty) :?> int
and set(value: int) = this.SetValue(_myProperty, value)
< /code>
Но это явно громоздко. Есть лучший способ?
Подробнее здесь: [url]https://stackoverflow.com/questions/79398430/nameof-limits-in-f[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия