代码说明:
int i = 5;
object obj = i;
byte b = (byte)obj; // X
Run Code Online (Sandbox Code Playgroud)
运行时,会在"X"行生成System.InvalidCastException("指定的强制转换无效").做双重演奏:
byte b = (byte)(int)obj;
Run Code Online (Sandbox Code Playgroud)
我本以为你应该能够将一个盒装的int(如果它的值在0..255范围内)转换成一个字节.任何人都可以对此有所了解吗?
(这是.net 2.0,如果重要的话).
这类似于:
但是担心在某些情况下从函数返回bool.
例如返回bool的函数:
public bool Poll()
{
bool isFinished = false;
// do something, then determine if finished or not.
return isFinished;
}
Run Code Online (Sandbox Code Playgroud)
像这样使用:
while (!Poll())
{
// do stuff during wait.
}
Run Code Online (Sandbox Code Playgroud)
从调用上下文来看,从Poll()返回的bool意味着什么并不明显.如果"轮询"功能被重命名为"IsFinished()",在某些方面可能更清楚,但该方法做了一些工作,并且(IMO)不会真正反映该功能实际上做了什么.像"IsFinished"这样的名字似乎也更适合于属性.另一种选择可能是将其重命名为:"PollAndReturnIsFinished",但这也感觉不对.
所以一个选项可能是返回枚举.例如:
public enum Status
{
Running,
Finished
}
public Status Poll()
{
Status status = Status.Running;
// do something, then determine if finished or not.
return status;
}
Run Code Online (Sandbox Code Playgroud)
这样称呼:
while (Poll() == Status.Running)
{
// do stuff during wait.
}
Run Code Online (Sandbox Code Playgroud)
但这感觉有点矫枉过正.有任何想法吗 ?
使用SQL Server 2008,Visual Studio 2005,.net 2.0 with SP2(支持新的SQL Server 2008数据类型).
我正在尝试编写一个SQLCLR函数,它将DateTime2作为输入并返回另一个DateTime2.例如:
using System;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
namespace MyCompany.SQLCLR
{
public class DateTimeHelpCLR
{
[SqlFunction(DataAccess = DataAccessKind.None)]
public static SqlDateTime UTCToLocalDT(SqlDateTime val)
{
if (val.IsNull)
return SqlDateTime.Null;
TimeZone tz = System.TimeZone.CurrentTimeZone;
DateTime res = tz.ToLocalTime(val.Value);
return new SqlDateTime(res);
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在,上面的编译很好.我希望这些SqlDateTimes映射到SQL Server的DateTime2,所以我尝试运行这个T-SQL:
CREATE function hubg.f_UTCToLocalDT
(
@dt DATETIME2
)
returns DATETIME2
AS
EXTERNAL NAME [SQLCLR].[MyCompany.SQLCLR.DateTimeHelpCLR].UTCToLocalDT
GO
Run Code Online (Sandbox Code Playgroud)
这会出现以下错误:
Msg 6551,级别16,状态2,过程f_UTCToLocalDT,第1行"f_UTCToLocalDT"的CREATE FUNCTION失败,因为返回值的T-SQL和CLR类型不匹配.
使用DATETIME(而不是DATETIME2)工作正常.但我宁愿使用DATETIME2来支持提高的精度.我做错了什么,或者SQLCLR没有(完全)支持DateTime2?
据报道,我们的网络应用程序出现了问题,这些问题似乎只发生在一个客户的网站上.出于诊断目的,您是否可以通过任何方式让客户端向您发送IE安全自定义设置?例如,在IE 8,工具 - > Internet选项 - >安全选项卡中,对于"Internet"区域,您可以单击"自定义级别"以查看自定义安全设置.用户可以通过某种方式将其设置导出到文本文件或其他内容吗?或者屏幕转储是唯一的方法吗?
编辑:对不起,应该提到它适用于Windows XP(SP3)
我在网上看到了一些jquery代码,采用了这种形式:
<script>
function doSomething(message)
{
$(document).ready(function(){
alert(message);
});
};
</script>
Run Code Online (Sandbox Code Playgroud)
即一个外部函数("doSomething"),里面有$(document).ready.我很困惑,因为当加载DOM时,不是$(document).ready下的代码?就像在函数内部有一个事件处理程序(?).这种形式的代码对任何人都有意义吗?谢谢.
举例说明:
public class Something
{
private static int number;
static Something()
{
int number = 10;
// Syntax to distingish between local variable and static variable ?
}
}
Run Code Online (Sandbox Code Playgroud)
在静态构造函数中,是否有一种语法可用于区分名为"number"的局部变量和同名的静态变量?
使用SQL Server/SSRS 2008.
使用SQL Server Profiler,我一直试图跟踪SSRS(rdl)报告生成调用到我的数据库的存储过程失败,即所以我可以看到传递了什么参数值等.
我应该寻找什么事件?
示例代码:
public class CA
{
public CA(string s, List<int> numList)
{
// do some initialization
}
public CA(string s, int num) : this(s, ListHelper.CreateList(num))
{
}
}
public static class ListHelper
{
public static List<int> CreateList(int num)
{
List<int> numList = new List<int>();
numList.Add(num);
return numList;
}
}
Run Code Online (Sandbox Code Playgroud)
"CA"中的第二个构造函数使用构造函数链接.在"this"调用中,我想将一个int转换为一个包含一个成员的List.代码通过辅助函数"CreateList"工作,但我想知道是否有比这更简洁的方法.即没有辅助方法,有没有办法做到这一点.
到目前为止,在这种情况下,我可能不会打扰使用构造函数链接.想法?
在Visual Studio 2012中使用Resharper 7.1.1.示例代码:
private string _str;
private string TheString
{
get
{
if (_str == null) // "X"
{
_str = GetString();
}
return _str;
}
}
// do some work to get string. e.g. read from database
private string GetString()
{
return "blah";
}
Run Code Online (Sandbox Code Playgroud)
在标记为"X"的行处,resharper强调"if"语句并建议"转换为??表达式".但是怎么样?我错过了什么吗?
例子:
create table dbo.t1 (id int)
if OBJECT_ID('dbo.s_Test') is not null drop proc dbo.s_Test
GO
create proc dbo.s_Test
as
create table #t2 (id2 int)
select t.id, t.xyz from dbo.t1 t join #t2 t2 on t2.id2 = t.id
GO
Run Code Online (Sandbox Code Playgroud)
创建 proc s_Test 时,我期待出现类似“无效的列名 'xyz',但在 proc 创建时没有错误。似乎 #temp 表似乎与它有关,就好像我创建了一样这个版本带有一个表变量:
if OBJECT_ID('dbo.s_Test2') is not null drop proc dbo.s_Test2
GO
create proc dbo.s_Test2
as
declare @t2 table (id2 int)
select t.id, t.xyz from dbo.t1 t join @t2 t2 on t2.id2 = t.id
GO
Run Code Online (Sandbox Code Playgroud)
我收到错误:列名“xyz”无效。关于为什么 …