如何检查变量的类型是否为Boolean类型?
我的意思是,有一些替代方案,如:
if(jQuery.type(new Boolean()) === jQuery.type(variable))
//Do something..
Run Code Online (Sandbox Code Playgroud)
但这对我来说似乎不太好看.
有没有更清洁的方法来实现这一目标?
假设我有以下变量:
System.Net.HttpStatusCode status = System.Net.HttpStatusCode.OK;
Run Code Online (Sandbox Code Playgroud)
如何检查这是成功状态代码还是失败状态代码?
例如,我可以执行以下操作:
int code = (int)status;
if(code >= 200 && code < 300) {
//Success
}
Run Code Online (Sandbox Code Playgroud)
我也可以有一些白名单:
HttpStatusCode[] successStatus = new HttpStatusCode[] {
HttpStatusCode.OK,
HttpStatusCode.Created,
HttpStatusCode.Accepted,
HttpStatusCode.NonAuthoritativeInformation,
HttpStatusCode.NoContent,
HttpStatusCode.ResetContent,
HttpStatusCode.PartialContent
};
if(successStatus.Contains(status)) //LINQ
{
//Success
}
Run Code Online (Sandbox Code Playgroud)
这些替代方案都没有说服我,我希望有一个.NET类或方法可以为我做这项工作,例如:
bool isSuccess = HttpUtilities.IsSuccess(status);
Run Code Online (Sandbox Code Playgroud) 我今天偶然发现了一种方法.我在谈论:Array.Initialize().
根据文件:
此方法旨在帮助编译器支持值类型数组; 大多数用户不需要这种方法.
该方法如何负责使编译器支持值类型?至于我关心这个方法只是:
通过调用值类型的默认构造函数来初始化value-type Array的每个元素.
另外,为什么公开?我不认为自己需要调用此方法,编译器在创建时已经初始化了数组,因此手动调用此方法将是多余的,无用的.
即使我的意图是重置数组的值,我仍然不会调用它,我会创建一个新的.array = new int[]
.
因此,似乎这种方法仅仅是为了编译器而存在.为什么是这样?谁能给我一些更多细节?
到现在为止,我的GET
方法如下所示:
protected override async Task<IHttpActionResult> GetAll(QueryData query)
{
// ... Some operations
//LINQ Expression based on the query parameters
Expression<Func<Entity, bool>> queryExpression = BuildQueryExpression(query);
//Begin to count all the entities in the repository
Task<int> countingEntities = repo.CountAsync(queryExpression);
//Reads an entity that will be the page start
Entity start = await repo.ReadAsync(query.Start);
//Reads all the entities starting from the start entity
IEnumerable<Entity> found = await repo.BrowseAllAsync(start, queryExpression);
//Truncates to page size
found = found.Take(query.Size);
//Number of entities returned in …
Run Code Online (Sandbox Code Playgroud) 假设我配置了这两个脚本包:
bundles.Add(new ScriptBundle("~/Scripts/Bootstrap").Include(
"~/Content/Scripts/jQuery/jquery-2.1.1.js",
"~/Content/Scripts/Bootstrap/bootstrap.js"));
bundles.Add(new ScriptBundle("~/Scripts/jQuery").Include(
"~/Content/Scripts/jQuery/jquery-2.1.1.js"));
Run Code Online (Sandbox Code Playgroud)
如您所见,~/Scripts/Boostrap
使用jQuery JavaScript文件和Bootstrap文件.这是因为Bootstrap需要jQuery才能工作.
另一方面,~/Scripts/jQuery
仅由jQuery文件组成.
我希望有两个捆绑包,以防视图只需要jQuery而不是Bootstrap.
但是,我在这里复制代码,我定义了两次 jQuery JavaScript文件路径.
有没有办法告诉~/Scripts/Boostrap
捆绑使用或"注入"另一个捆绑?
像这样的东西:
bundles.Add(new ScriptBundle("~/Scripts/Bootstrap").UseBundle("~/Scripts/jQuery").Include(
"~/Content/Scripts/Bootstrap/bootstrap.js"));
Run Code Online (Sandbox Code Playgroud) 有没有办法让配置部分用JSON而不是XML编写?
我们假设我有以下内容ConfigurationSection
:
public class UsersConfig : ConfigurationSection {
[ConfigurationProperty("users",
IsRequired = false)]
public UserCollection Users {
get { return this["users"] as UserCollection; }
}
}
[ConfigurationCollection(typeof(UserElement),
AddItemName = "user"]
public class UsersCollection : ConfigurationElementCollection {
protected override ConfigurationElement CreateNewElement() {
return new UserElement();
}
protected override object GetElementKey(ConfigurationElement element) {
return ((UserElement)element).Name;
}
}
public class UserElement : ConfigurationElement {
[ConfigurationProperty("name",
IsRequired = true,
IsKey = true)]
public string Name {
get { return this["name"] as string; } …
Run Code Online (Sandbox Code Playgroud) 考虑以下程序:
public class Program
{
private static Random _rnd = new Random();
private static readonly int ITERATIONS = 5000000;
private static readonly int RANDOM_MAX = 101;
public static void Main(string[] args)
{
ConcurrentDictionary<int,int> dic = new ConcurrentDictionary<int,int>();
Parallel.For(0, ITERATIONS, _ => dic.AddOrUpdate(_rnd.Next(1, RANDOM_MAX), 1, (k, v) => v + 1));
foreach(var kv in dic)
Console.WriteLine("{0} -> {1:0.00}%", kv.Key, ((double)kv.Value / ITERATIONS) * 100);
}
}
Run Code Online (Sandbox Code Playgroud)
这将打印以下输出:
(注意每次执行时输出会有所不同)
> 1 -> 97,38%
> 2 -> 0,03%
> 3 -> …
Run Code Online (Sandbox Code Playgroud) 我在互联网上找不到答案.
假设我有一个DbContext
,我只是从中选择所有实体.我不添加,更新或删除任何实体DbSet
.
如果我SaveChanges
事后打电话给DbSet
.它是否真的浪费资源建立连接和其他东西即使我没有做任何改变DbSet
?
是否足够智能来检测是否进行了更改,并且行为方式不同?
常量表达式是一个可以在编译时完全计算的表达式.因此,引用类型常量的唯一可能值是string和null引用.
根据:typeof(T)与Object.GetType()的性能,typeof(T)
是一个编译时表达式.
那么为什么不能Type
成为一个恒定的价值呢?
以下代码将无法编译:
public const Type INT_TYPE = typeof(int);
Run Code Online (Sandbox Code Playgroud) 这是该Ok()
方法的签名ApiController
:
protected internal virtual OkResult Ok();
Run Code Online (Sandbox Code Playgroud)
这是我RestController
班上的方法(从中扩展而来ApiController
):
// Note that I'm not overriding base method
protected IHttpActionResult Ok(string message = null);
Run Code Online (Sandbox Code Playgroud)
从OkResult
implements开始IHttpActionResult
,这两种方法都可以像这样调用:
IHttpActionResult result = Ok();
Run Code Online (Sandbox Code Playgroud)
事实上,这就是我在我的应用程序中所做的.
我的类PersistenceRestController
(从中扩展而来RestController
)具有以下代码行:
protected override async Task<IHttpActionResult> Delete(Key id)
{
bool deleted = //... Attempts to delete entity
if(deleted) return Ok();
else return NotFound();
}
Run Code Online (Sandbox Code Playgroud)
编译很好,没有关于方法歧义的警告.这是为什么?
PersistenceRestController
还继承了受保护的方法,ApiController
所以它应该有两个版本Ok()
(它确实).
在执行时,执行的方法是我的方法RestController
.
编译器如何知道要运行哪种方法?
c# ×9
.net ×6
asp.net ×3
types ×2
arrays ×1
asp.net-mvc ×1
c#-4.0 ×1
const ×1
dbcontext ×1
http-headers ×1
javascript ×1
jquery ×1
json ×1
methods ×1
overloading ×1
random ×1
reference ×1
system.net ×1
xml ×1