実際のコードと少し違いますが、以下のようなコードが動きません。Commandクラスは実際には継承されていろいろなクラスが存在しています。
ジェネリック型の指定をしてあるため、動くと思ったのですが、
[CS1503]引数 1:は’T’から’Command'へ変換することはできません。
というエラーがでます。
Rider上でのエラーは
Argument type 'T' is not assignable to parameter type 'Command'
※名前空間の表記は省略してあり、違う名前空間上の同名クラスは存在しておりません。
何か可能性としてあり得ることをご教授願えると幸いです。
csharp
1public class ItemViewModel<T>: ViewModel where T: Command, new() 2{ 3 public Response Write() 4 { 5 return response = Tcp.Read(new T()); 6 } 7} 8 9public static class Tcp{ 10 public static Response Read(Command command) 11 { 12 return TcpMaster.Execute(command); 13 } 14} 15
- .NET Framework
=> 4.6.1
- C#
=> ver.7.0
追記
csharp
1 2using System; 3using System.Net; 4using System.Threading; 5using Framework.TCP; 6using Common.Model; 7 8namespace TcpIp.Model 9{ 10 public static class Tcp 11 { 12 private static MasterTCP _masterTcp; 13 14 private static IPAddress _ipAddress; 15 private static int _port; 16 private static ushort _address; 17 private static byte _unitId; 18 private static bool _updateFlag; 19 private static int _sleepTime; 20 21 public static void InitSettings(string ipAddr, int port, byte unitId, ushort address) 22 { 23 _ipAddress = IPAddress.Parse(ipAddr); 24 _port = port; 25 _address = address; 26 _unitId = unitId; 27 _updateFlag = true; 28 _sleepTime = 2500; 29 _masterTcp = new MasterTCP(_ipAddress.ToString(), _port); 30 } 31 32 public static void Connect() 33 { 34 if (_ipAddress == null && _port == 0) 35 throw new Exception("接続情報が初期化されていません。"); 36 if (_masterTcp.Connected) 37 return; 38 _masterTcp.Connect(); 39 } 40 41 public static void DisConnect() 42 { 43 if (!_masterTcp.Connected) 44 return; 45 _masterTcp.Disconnect(); 46 } 47 48 public static void TryOpen() 49 { 50 try 51 { 52 _masterTcp.Connect(); 53 } 54 catch (Exception ex) 55 { 56 throw new DeviceIoFailedException(""); 57 } 58 } 59 private static void Execute(Command commandClass, TcpFunction function) 60 { 61 _updateFlag = !_updateFlag; 62 var command = new byte[2]; 63 switch (function) 64 { 65 case TcpFunction.Read: 66 command = commandClass.CreateReadCommand(); 67 break; 68 case TcpFunction.Write: 69 command = commandClass.CreateWriteCommand(); 70 break; 71 default: 72 throw new Exception("[Tcp.Execute] 無効な操作です。"); 73 } 74 var value = (ushort) ((_updateFlag ? 1 : 0 >> 15) + (command[0] >> 8) + command[1]); 75 _masterTcp.WriteSingleRegister(_unitId, (ushort)(1056 + _address), value); 76 77 Thread.Sleep(_sleepTime); 78 } 79 80 public static void Read(Command command) 81 { 82 Execute(command, TcpFunction.Read); 83 } 84 85 public static void Write(Command command) 86 { 87 Execute(command, TcpFunction.Write); 88 } 89 } 90 91 public enum TcpFunction 92 { 93 Read, 94 Write 95 } 96}
こちらが実際のTcpクラスになります。
残念ながら、完全に同一ではなく、多少名前空間などを変えてあります。
csharp
1using System; 2using System.Collections.ObjectModel; 3using System.Windows.Media; 4using Common.Model; 5using TcpIp.Model; 6 7namespace TcpIp.Ui.ViewModels 8{ 9 public class ItemViewModel<T> : ViewModel where T : Command , new() 10 { 11 protected Brush signal; 12 public string ItemName { get; protected set; } 13 14 public MainViewModel.Response Write() 15 { 16 var isWritten = false; 17 var isRead = false; 18 try 19 { 20 Tcp.Read(new T()); 21 DoEvents(); 22 isWritten = true; 23 } 24 catch (Exception) 25 { 26 DoEvents(); 27 28 isWritten = false; 29 } 30 31 try 32 { 33 var bytes = Tcp.Read(new T()); 34 Index = new T().CreateFromHid(bytes).UiValue; 35 Signal = new SolidColorBrush(Color.FromArgb(0xff, 0x00, 0xff, 0x00)); 36 37 isRead = true; 38 } 39 catch (Exception) 40 { 41 DoEvents(); 42 43 isRead = false; 44 } 45 46 if (!isWritten || !isRead) 47 { 48 Signal = Brushes.Red; 49 } 50 51 if (!isWritten && !isRead) 52 { 53 return new MainViewModel.Response() 54 { 55 ItemName = ItemName, 56 Exception = new ApiException("において、読み書きが失敗しました。") 57 }; 58 } 59 if (isWritten && !isRead) 60 { 61 return new MainViewModel.Response() 62 { 63 ItemName = ItemName, 64 Exception = new ApiException("において、読込が失敗しました。") 65 }; 66 } 67 if (!isWritten) 68 { 69 return new MainViewModel.Response() 70 { 71 ItemName = ItemName, 72 Exception = new ApiException("において、書き込みが失敗しました。") 73 }; 74 } 75 return null; 76 } 77}
こちらが、ItemViewModelクラスになります。
回答1件
あなたの回答
tips
プレビュー