试图制作Feature
通用然后突然编译说
接线员'?' 不能应用于'T'类型的操作数
这是代码
public abstract class Feature<T>
{
public T Value
{
get { return GetValue?.Invoke(); } // here is error
set { SetValue?.Invoke(value); }
}
public Func<T> GetValue { get; set; }
public Action<T> SetValue { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
可以使用此代码
get
{
if (GetValue != null)
return GetValue();
return default(T);
}
Run Code Online (Sandbox Code Playgroud)
但我想知道如何修复那个漂亮的C#6.0单线程.
我正在玩弄yield
和IEnumerable
,我现在好奇为什么或如何以下代码段工作:
public class FakeList : IEnumerable<int>
{
private int one;
private int two;
public IEnumerator<int> GetEnumerator()
{
yield return one;
yield return two;
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
Run Code Online (Sandbox Code Playgroud)
现在编译器如何转变:
public IEnumerator<int> GetEnumerator()
{
yield return one;
yield return two;
}
Run Code Online (Sandbox Code Playgroud)
变成了IEnumerator<int>
?
是否存在需要公共只读字段与仅获取自动实现属性的情况?
public class Foo
{
public readonly string Hello;
public string Hello2 { get; }
}
Run Code Online (Sandbox Code Playgroud)
两者都只能在构造函数中设置,并且都提供了类外的只读访问权限.我有点累了所以我可能会遗漏一些东西.
我有一个具有一系列属性的对象.当我得到特定实体时,我可以看到我正在寻找的字段(opportunityid
),并且它的Value
属性就是Guid
这个机会.这是我想要的值,但它并不总是一个机会,因此我不能总是看opportunityid
,所以我需要根据用户提供的输入获取字段.
到目前为止我的代码是:
Guid attrGuid = new Guid();
BusinessEntityCollection members = CrmWebService.RetrieveMultiple(query);
if (members.BusinessEntities.Length > 0)
{
try
{
dynamic attr = members.BusinessEntities[0];
//Get collection of opportunity properties
System.Reflection.PropertyInfo[] Props = attr.GetType().GetProperties();
System.Reflection.PropertyInfo info = Props.FirstOrDefault(x => x.Name == GuidAttributeName);
attrGuid = info.PropertyType.GUID; //doesn't work.
}
catch (Exception ex)
{
throw new Exception("An error occurred when retrieving the value for " + attributeName + ". Error: " + ex.Message);
}
}
Run Code Online (Sandbox Code Playgroud)
动态attr …
我试图使用async/wait .NET功能进行Oracle查询.结果集非常大,需要大约5-10秒才能恢复.Window_Loaded挂起了UI线程,本质上我想使用async/wait在后台进行查询,然后使用结果更新数据视图.
这是Oracle驱动程序问题还是代码错误?例如,这是同步而不是异步完成的事情吗?我正在使用Oracle.ManagedDataAccess
Oracle可以从Oracle网站获得的最新信息.
async Task<DataTable> AccessOracleAsync()
{
DataTable dt;
using(OracleConnection conn = new OracleConnection(ConfigurationManager.ConnectionStrings["connStr"].ConnectionString))
using (OracleCommand cmd = new OracleCommand(@"SELECT * FROM myTbl", conn))
{
await conn.OpenAsync();
using (var reader = await cmd.ExecuteReaderAsync())
{
dt = new DataTable();
dt.Load(reader);
}
}
return dt;
}
private async void Window_Loaded(object sender, RoutedEventArgs e)
{
await AccessOracleAsync();
}
Run Code Online (Sandbox Code Playgroud)
我试过这个,它仍然在用户界面陷入僵局:
async Task<DataView> AccessOracleAsync()
{
DataTable dt;
using (OracleConnection conn = new OracleConnection(ConfigurationManager.ConnectionStrings["connStr"].ConnectionString))
using (OracleCommand cmd = new OracleCommand(@"SELECT * FROM myTbl", conn)) …
Run Code Online (Sandbox Code Playgroud) 我已经定义了以下内容enum
:
public enum DeviceType
{
[Description("Set Top Box")]
Stb = 1,
Panel = 2,
Monitor = 3,
[Description("Wireless Keyboard")]
WirelessKeyboard = 4
}
Run Code Online (Sandbox Code Playgroud)
我正在利用该Description
属性允许我提取更多用户可读的枚举版本以在UI中显示.我使用以下代码获得描述:
var fieldInfo = DeviceType.Stb.GetType().GetField(DeviceType.Stb.ToString());
var attributes = (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
var description = (attributes.Length > 0 ? attributes[0].Description : DeviceType.Stb.ToString());
Run Code Online (Sandbox Code Playgroud)
上面的代码将给我:description = "Set Top Box"
.如果没有Description
设置属性,它将为我提供枚举的字符串值.
我现在想为每个枚举添加第二个/自定义属性(为了示例,称为"值").例如:
public enum DeviceType
{
[Description("Set Top Box")]
[Value("19.95")]
Stb = 1,
[Value("99")]
Panel = 2,
[Value("199.99")]
Monitor = 3,
[Description("Wireless Keyboard")]
[Value("20")]
WirelessKeyboard …
Run Code Online (Sandbox Code Playgroud) SignalR适用于Windows桌面应用程序(winforms/wpf)吗?
使用SignalR与Windows桌面应用程序有什么优缺点?
有任何性能考虑因素吗?
我想在服务器和许多客户端之间建立实时连接.连接将是不变的.
在我的项目中,我想在我现有的切换代码上添加一个文本.所以我想这样,当切换为ON时,它应显示文本"ON"并显示"OFF"文本,如果切换关闭.我无法将其更改为其他切换,因为它已经有一个使用它的后端.我只想输入"ON"和"OFF"文本.谢谢.
这是我的代码HTML:
<label class="switch"><input type="checkbox" id="togBtn"><div class="slider round"></span></div></label>
Run Code Online (Sandbox Code Playgroud)
CSS:
.switch input {display:none;}
.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ca2222;
-webkit-transition: .4s;
transition: .4s;
}
.slider:before {
position: absolute;
content: "";
height: 26px;
width: 26px;
left: 4px;
bottom: 4px;
background-color: white;
-webkit-transition: .4s;
transition: .4s;
}
input:checked + .slider {
background-color: #2ab934;
}
input:focus + .slider {
box-shadow: 0 0 1px #2196F3;
}
input:checked + .slider:before {
-webkit-transform: translateX(26px);
-ms-transform: translateX(26px);
transform: …
Run Code Online (Sandbox Code Playgroud) 鉴于以下代码,我想知道为什么在编译失败时referenceValue = ConstantInt;
有效referenceValue = StaticInt;
.
namespace Demo
{
public class Class1
{
private const int ConstantInt = 42;
private static readonly int StaticInt = 42;
public void DemoMethod(ref uint referenceValue)
{
referenceValue = ConstantInt; // This compiles
referenceValue = StaticInt; // This claims that the source type 'int' cannot be converted to 'unit' implicitly.
}
}
}
Run Code Online (Sandbox Code Playgroud) 我希望有一个原因,我根本就不知道.为什么要typeof(string[][,])
回String[,][]
名?
c# ×9
.net ×3
c#-6.0 ×2
async-await ×1
constants ×1
css ×1
delegates ×1
enums ×1
generics ×1
html ×1
ienumerable ×1
oracle ×1
propertyinfo ×1
reflection ×1
signalr ×1
static ×1
toggle ×1
types ×1
winforms ×1
wpf ×1
yield-return ×1