wpf

dynamic property 에서 Set을 할떄

동동(이재동) 2022. 12. 5. 11:44

await SendConfirmMsg(msgSplit, DataManager.LocalData.LongTrendBuy, "QuitRange");

 

 

 

private static async Task SendConfirmMsg(string[] msgSplit, dynamic data, string prop)
        {
            double numValue;
            bool isNum = double.TryParse(msgSplit[2], out numValue);

            data.GetType().GetProperty(prop).SetValue(data, numValue, null);           

            DataManager.Instance.SaveData();
            await TelegramPushService.SendMessage($"{msgSplit[0]} {msgSplit[1]} {numValue}");
        }

 

 

이런식으로 반복되는 구문을 dynamic을 이용해서 짧게 만들수 있다.

 

중요

 

data.GetType().GetProperty(prop).SetValue(data, numValue, null);   

 

참고

https://stackoverflow.com/questions/12970353/c-sharp-dynamically-set-property