根据C#规范10.4 Constants:
常量声明中指定的类型必须是 sbyte,byte,short,ushort,int,uint,long,ulong,char,float,double,decimal,bool,string,enum-type 或reference-type.每个常量表达式必须生成目标类型或可通过隐式转换(第6.1节)转换为目标类型的类型的值.
为什么我不能做以下事情:
public class GenericClass<T>
where T : class
{
public const T val = null;
}
Run Code Online (Sandbox Code Playgroud)
这应该是可能的,因为:
where T : class意思是,The type argument must be a reference type; this applies also to any class, interface, delegate, or array type(来自MSDN)stringis 之外的引用类型常量的唯一可能值null.任何可能的解释?
我目前正在尝试将我的windowsphone Time App本地化为几个国家.我正在使用Noda Time,因为这对新手来说非常容易.我面临的问题是所有Timezone Id都是标准英语,我正在寻找一种方法来将这些Id转换为本地语言字符串.
一种方法是为每种语言中的每个ID制作本地化字符串.但由于有500个时区,它似乎非常低效.请建议一种让我直接将TimeZone ID转换为本地语言的方法.
我的代码:
var now = Instant.FromDateTimeUtc(DateTime.UtcNow);
var tzdb = DateTimeZoneProviders.Tzdb;
var list = from id in tzdb.Ids
where id.Contains("/") && !id.StartsWith("etc", StringComparison.OrdinalIgnoreCase)
let tz = tzdb[id]
let offset = tz.GetUtcOffset(now)
orderby offset, id
select new
{
DisplayValue = string.Format("(UTC{0}) {1} {2} ", offset.ToString("+HH:mm", null), now.WithOffset(offset).TimeOfDay.ToString("hh:mm tt",null),id)
};
Run Code Online (Sandbox Code Playgroud) 我正在尝试从Luis创建一个子对话框,旨在从用户那里收集更多信息.但是我收到了一个无法从'方法组'转换为'ResumeAfter <object>'上下文的第二个参数的错误消息.
[LuisIntent("Login")]
public async Task LoginIntent(IDialogContext context, LuisResult result)
{
var serverdialog = new ServerDialog();
await context.Call(serverdialog, ResumeAfterServerDialog); //error here
}
private async Task ResumeAfterServerDialog(IDialogContext context, IAwaitable<string> serverName)
{
this.serverAddress = await serverName;
await context.PostAsync($"you've entered {this.serverAddress}");
context.Wait(MessageReceived);
}
Run Code Online (Sandbox Code Playgroud)
服务器对话框类是
[Serializable]
public class ServerDialog : IDialog<object>
{
public async Task StartAsync(IDialogContext context)
{
await context.PostAsync("Enter your server's name (example: 10.10.10.52)");
context.Wait(ReceiveServerDialog);
}
public async Task ReceiveServerDialog(IDialogContext context, IAwaitable<IMessageActivity> result)
{
IMessageActivity message = await result;
context.Done(message.Text);
}
}
Run Code Online (Sandbox Code Playgroud)
我找到了一个解释说: …
我有一个关于为for循环内的变量赋值的问题.据我所知,当有可能在尚未分配变量时,编译器会发出此错误消息,如Microsoft所述.
请注意,当编译器遇到可能导致使用未分配变量的构造时,即使您的特定代码没有,也会生成此错误.
我的代码看起来像这样:
static void Main(string[] args)
{
int i;
for (int j = 0; j <= 5; j++)
{
i = j;
}
Console.WriteLine(i.ToString());
Console.ReadLine();
}
Run Code Online (Sandbox Code Playgroud)
我认为即使在这个特定的场景中i会被赋值,编译器也不会检查for语句中的实际条件,这意味着它会对待它
for (int j = 0; j <= -1; j++)
Run Code Online (Sandbox Code Playgroud)
一样的?
我有一个EF6查询,它获取ID列表并执行查询:
public IList<Audit> AuditsByIDs(List<int> ids)
{
return _db.Audits
.Include(p => p.User)
.Where(p => ids.Any(i => i == p.Id)).ToList();
}
Run Code Online (Sandbox Code Playgroud)
它适用于少量的ID,但当达到数百时,我得到错误:
SQL语句的某些部分嵌套得太深.重写查询或将其分解为较小的查询.
如何让查询只返回传入的ID?我无法更改数据库:(
我在尝试发送电子邮件异步时遇到了一个问题,我发现Stackoverflow上没有帖子,但没有一个是有帮助的.我有以下代码块
public class EmailService : IIdentityMessageService
{
public Task SendAsync(IdentityMessage message)
{
// Plug in your email service here to send an email.
var mailMessage = new MailMessage
("me@example.com", message.Destination, message.Subject, message.Body);
mailMessage.IsBodyHtml = true;
var client = new SmtpClient();
client.SendCompleted += (s, e) => client.Dispose();
client.SendAsync(mailMessage,null);
return Task.FromResult(0);
}
}
Run Code Online (Sandbox Code Playgroud)
我收到了一封电子邮件,但在这段代码运行时遇到异常
an asynchronous module or handler completed while an asynchronous operation was still pending.
Run Code Online (Sandbox Code Playgroud)
有什么建议吗?
我在基类上有一个虚方法.每次我覆盖它,我想返回不同的对象类型.我知道这是不可能的,但处理这种情况的最佳方法是什么?
基本方法示例:
public virtual void clickContinue()
{
//click the continue button
}
Run Code Online (Sandbox Code Playgroud)
以及覆盖它的方法:
public override myObject clickContinue()
{
//click then continue button
//return myObject
}
Run Code Online (Sandbox Code Playgroud)
我需要做几个类似的覆盖,都返回不同的对象.同样,我知道这不可能按照上面的方式完成 - 我试图找出处理这种情况的最佳方法.
我不应该得到负数,请看下面的截图:
见下图:

这是代码:
for (double i=8.0; i<=12;i=i+0.5)
{
double aa= (i - Convert.ToInt32(i)) ;
Console.WriteLine(" "+i+" "+aa);
}
Run Code Online (Sandbox Code Playgroud) 我有这段代码:
private object DeserialiseFromXMLFile(string fileLocation, Type type)
{
XmlSerializer serializer = new
XmlSerializer(type);
FileStream fs = new FileStream(fileLocation, FileMode.Open);
XmlReader reader = new XmlTextReader(fs);
return serializer.Deserialize(reader);
}
Run Code Online (Sandbox Code Playgroud)
我想知道我是否使用泛型,因为我希望返回类型为T
有谁知道这是可能的还是这是最优雅的解决方案?
提前致谢
我有一个使用通用属性的类.例如:
class Person
{
public MyGenericProperty<string> Field1
{
get { return field1; }
set { field1 = value; }
}
private MyGenericProperty<string> field1= new MyInheritedGenericProperty<string>("Alan1");
}
Run Code Online (Sandbox Code Playgroud)
我想在另一个类中使用这个类和反射,我有一个类似的方法
public void DoSomethingWithProperty(object sourceobject)
{
foreach (var aProperty in sourceobject.GetType().GetProperties())
{
*if(aProperty.PropertyType == typeof(MyGenericProperty<>))*
{
*var obj = (MyGenericProperty<>)aProperty.GetValue(sourceobject, null);*
}
}
return null;
}
Run Code Online (Sandbox Code Playgroud)
我有两个问题
1-如何进行通用属性的类型检查.在那个示例代码中if(aProperty.PropertyType == typeof(MyGenericProperty<>))不起作用.
MyGenericProperty的2-T可以是任何类,如何在不通过反射知道T的情况下转换MyGenericProperty类
var obj = (MyGenericProperty<>)aProperty.GetValue(sourceobject, null);
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助.
c# ×10
generics ×3
.net ×1
azure-language-understanding ×1
botframework ×1
console ×1
const ×1
for-loop ×1
identity ×1
linq ×1
nodatime ×1
reflection ×1
sql ×1