小编Dan*_*son的帖子

调用泛型扩展方法而不指定任意类型

这是我用于测试Type扩展方法的类的片段:

class Something
{
  [StringLength(100, MinimumLength = 1, ErrorMessage = "Must have between 1 and 100 characters")]
  public string SomePublicString { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我有以下扩展方法:

public static class TypeExtensions
{
  public static TAttributeType GetCustomAttribute<T, TAttributeType, TProperty>(this T value, Expression<Func<T, TProperty>> propertyLambda, bool inherit = false)
  {
    var type = typeof(T);
    var member = (MemberExpression)propertyLambda.Body;
    var propertyInfo = (PropertyInfo)member.Member;
    var customAttributes = propertyInfo.GetCustomAttributes(typeof(TAttributeType), inherit);

    return customAttributes.OfType<TAttributeType>().FirstOrDefault();
  }
}
Run Code Online (Sandbox Code Playgroud)

用于单元测试:

1:  var something = new Something();
2:  var actual = something.GetCustomAttribute<Something, …
Run Code Online (Sandbox Code Playgroud)

c# lambda extension-methods

5
推荐指数
1
解决办法
193
查看次数

如何使用XML Serializer将多个xml命名空间应用于同一个类

我正在尝试使用WCF生成代理类,当序列化时,将下面的类和实例转换为下面的XML.

但是,当我尝试将命名空间应用于类时,它们被错误地插入,或者根本不插入.

我究竟做错了什么?我怎样才能解决这个问题?

提前谢谢了.

班级结构:

[XmlRoot]
public class Request
{
  public int Id

  public Name Name {get;set;}
}

[XmlRoot]
public class Name
{
  [XmlAttribute]
  public bool test {get;set;}

  public string FirstName {get;set;}

  public string LastName {get;set;}
}
Run Code Online (Sandbox Code Playgroud)

期望的XML结构(多余的XML修剪):

<x1:Request xmlns:x1="Data/Main" xmlns:x2="Data/All">
  <x2:Id>0</x2:Id>
  <x2:Name test="true">
    <x2:FirstName>Dan</x2:FirstName>
    <x2:LastName>Atkinson</x2:LastName>
  </x2:Name>
</x1:Request>
Run Code Online (Sandbox Code Playgroud)

c# xml-serialization

4
推荐指数
1
解决办法
6503
查看次数

正则表达式以匹配日期范围

有没有人对是否/如何创建正则表达式以匹配任何给定时期的日期有任何想法?

两个例子:

23/11/2008 - 12/04/2010

//The expression would evaluate whether 16/04/2010 was in this range, and return false.
//The expression would determine whether 03/12/2009 was in this range, and return true.
Run Code Online (Sandbox Code Playgroud)

01/09/1984 - 30/04/2001

//The expression would evaluate whether 16/04/1990 was in this range, and return true.
//The expression would determine whether 03/12/2009 was in this range, and return false.
Run Code Online (Sandbox Code Playgroud)

我一直在绞尽脑汁想知道如何提出一些东西,但我没有什么可以接近的.Web上的示例仅对检查日期是否采用特定格式感兴趣,而不关心验证范围.

我在这里标记C#的原因是,这不能在直接正则表达式中完成,并且需要为每个单独的情况手动构建范围正则表达式.

c# regex

4
推荐指数
4
解决办法
8016
查看次数

带有Expires set的HttpCookie返回DateTime.MinValue

我在设置cookie时看到了一些奇怪的东西......

行动:

string cookieName = "foo";
string cookieValue = "bar";

//Set a cookie in the response, along with the Expires.
this.ControllerContext.HttpContext.Response.Cookies.Add(
  new HttpCookie(cookieName, cookieValue)
  {
    Expires = DateTime.Now.AddHours(1)
  }
);
Run Code Online (Sandbox Code Playgroud)

在调试时,我可以看到这个新cookie将来会有一个小时到期,然而,当我在视图中查看cookie时,到期时间不存在......

视图:

<%= Request.Cookies.Get("foo").Value %>
Run Code Online (Sandbox Code Playgroud)

退货bar.

<%= Request.Cookies.Get("foo").Expires %>
Run Code Online (Sandbox Code Playgroud)

返回 01/01/0001 00:00:00

有任何想法吗?!

c# cookies asp.net-mvc

4
推荐指数
1
解决办法
3280
查看次数

Microsoft.Practices.Unity.ResolutionFailedException

安装我的WPF应用程序后,我尝试运行该应用程序,它崩溃并在事件查看器中记录下面的错误.有人有想法吗?

'----------------------------------------------------------------------

Application: MyApp.Windows.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: Microsoft.Practices.Unity.ResolutionFailedException
Stack:
   at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(System.Type, System.Object, System.String, System.Collections.Generic.IEnumerable`1<Microsoft.Practices.Unity.ResolverOverride>)
   at Microsoft.Practices.Unity.UnityContainer.Resolve(System.Type, System.String, Microsoft.Practices.Unity.ResolverOverride[])
   at Microsoft.Practices.Unity.UnityContainerExtensions.Resolve[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]](Microsoft.Practices.Unity.IUnityContainer, Microsoft.Practices.Unity.ResolverOverride[])
   at MyApp.Windows.IoC.Resolve[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]()
   at MyApp.Windows.Navigation.NavigationController.Navigate(System.String, System.Windows.Controls.UserControl)
   at MyApp.Windows.LoginWindow..ctor()
   at MyApp.Windows.App.OnStartup(System.Windows.StartupEventArgs)
   at System.Windows.Application.<.ctor>b__1(System.Object)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate, System.Object, Int32)
   at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(System.Object, System.Delegate, System.Object, Int32, System.Delegate)
   at System.Windows.Threading.DispatcherOperation.InvokeImpl()
   at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(System.Object)
   at System.Threading.ExecutionContext.runTryCode(System.Object)
   at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode, CleanupCode, System.Object)
   at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
   at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, …
Run Code Online (Sandbox Code Playgroud)

unity-container

4
推荐指数
1
解决办法
7837
查看次数

jQuery集中的每个第n个元素

我想在jQuery元素集中选择每个nth.

例如.

<ul>
  <li>A</li>
  <li>B</li>
  <li>C</li>
  <li>D</li>
  <li>E</li>
  <li>F</li>
  <li>G</li>
</ul>
Run Code Online (Sandbox Code Playgroud)
  1. 如何选择每个第三个(C和F)元素?
  2. 如何选择同一组中不是每三个(A,B,D,E,G)的所有其他元素?

jquery

4
推荐指数
2
解决办法
7510
查看次数

提高代码序列化速度的更好方法

我有以下代码将List序列化为字节数组,以通过Web服务进行传输。该代码在较小的实体上工作相对较快,但这是一个大约60,000个项目的列表。执行formatter.Serialize方法需要几秒钟。无论如何要加快速度?

public static byte[] ToBinary(Object objToBinary)
{
    using (MemoryStream memStream = new MemoryStream())
    {
        BinaryFormatter formatter = new BinaryFormatter(null, new StreamingContext(StreamingContextStates.Clone));
        formatter.Serialize(memStream, objToBinary);
        memStream.Seek(0, SeekOrigin.Begin);
        return memStream.ToArray();
    }
}
Run Code Online (Sandbox Code Playgroud)

c# performance serialization visual-studio-2005

3
推荐指数
1
解决办法
3346
查看次数

.net MVC 3请求事件

我对.net有点新意,并试图掌握一些概念.

我已经在Coldfusion中写了一段时间,在CF中,在Application.cfc下有一个名为onRequest()的事件,每当有一个页面时就会触发.

.net中的内容用于捕获请求信息?

而且有没有办法锁定或扩展Request事件以触发我自己的事件?

.net asp.net asp.net-mvc asp.net-mvc-3

3
推荐指数
1
解决办法
4956
查看次数

的JavaScript.附加到blob

因为不推荐使用BlobBuilder,所以我们必须使用Blob,而不是

var bb = new (window.MozBlobBuilder || window.WebKitBlobBuilder || window.BlobBuilder)();
bb.append(data);
var blob = bb.getBlob();
Run Code Online (Sandbox Code Playgroud)

我们的确是

var blob = new Blob([data]);
Run Code Online (Sandbox Code Playgroud)

没关系,但是如果我想将数据追加到同一个blob上会怎么样呢?就像是:

for(var i=0;i<10;i++){
    bb.append(" "+i);
}
Run Code Online (Sandbox Code Playgroud)

没有BlobBuilder怎么做?谢谢大家!

javascript blob

3
推荐指数
1
解决办法
5224
查看次数

C#中的循环赛

我遇到了一个问题(可能是由于睡眠不足!),我正在尝试解决C#中的数学问题.

假设我有一台饮料机,我有三排可以装满可乐的空行.我手里拿着17罐可乐,我必须每次填充一排.

例如...

通过1:

将可乐添加到第1行.饮料= 1
将可乐添加到第2行.饮料= 1
将可乐添加到第3行.饮料= 1

通过2:

将可乐添加到第1行.饮料= 2
将可乐添加到第2行.饮料= 2
将可乐添加到第3行.饮料= 2

...

通过6

将可乐添加到第1行.饮料= 6
将可乐添加到第2行.饮料= 6
将可乐添加到第3行.饮料= 5(此时不再有饮料)

出于某种原因,我完全迷失了.有人可以帮忙吗?!

c# logic

2
推荐指数
1
解决办法
8648
查看次数