一位同事今天问我如何为一个系列添加一个范围.他有一个继承自的课程Collection<T>.这种类型的get-only属性已包含一些项目.他想将另一个集合中的项目添加到属性集合中.他怎么能以C#3友好的方式这样做呢?(注意关于get-only属性的约束,这会阻止像执行Union和重新分配这样的解决方案.)
当然,与物业的foreach.添加将工作.但是A List<T>风格的AddRange会更加优雅.
编写扩展方法很容易:
public static class CollectionHelpers
{
public static void AddRange<T>(this ICollection<T> destination,
IEnumerable<T> source)
{
foreach (T item in source)
{
destination.Add(item);
}
}
}
Run Code Online (Sandbox Code Playgroud)
但我感觉我正在重新发明轮子.我没有发现任何类似System.Linq或morelinq.
糟糕的设计?只需致电添加?缺少明显的?
这是设置.假设我有一些需要服务实例的动作过滤器:
public interface IMyService
{
void DoSomething();
}
public class MyService : IMyService
{
public void DoSomething(){}
}
Run Code Online (Sandbox Code Playgroud)
然后我有一个需要该服务实例的ActionFilter:
public class MyActionFilter : ActionFilterAttribute
{
private IMyService _myService; // <--- How do we get this injected
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
_myService.DoSomething();
base.OnActionExecuting(filterContext);
}
}
Run Code Online (Sandbox Code Playgroud)
在MVC 1/2中,将依赖关系注入动作过滤器是一个痛苦的屁股.最常见的方法是使用自定义操作调用因为在这里可以看到:http://www.jeremyskinner.co.uk/2008/11/08/dependency-injection-with-aspnet-mvc-action-filters/的这种解决方法背后的主要动机是因为以下方法被认为是与容器的草率和紧密耦合:
public class MyActionFilter : ActionFilterAttribute
{
private IMyService _myService;
public MyActionFilter()
:this(MyStaticKernel.Get<IMyService>()) //using Ninject, but would apply to any container
{
}
public MyActionFilter(IMyService myService)
{
_myService = myService;
} …Run Code Online (Sandbox Code Playgroud) c# asp.net-mvc dependency-injection actionfilterattribute asp.net-mvc-3
我已经阅读了类似的帖子,他们没有回答我的问题.
在C#中,我有一个我从WebClient.DownloadString获取的字符串.我已经尝试将client.Encoding设置为新的UTF8Encoding(false),但这没有任何区别 - 我仍然在结果字符串的开头以UTF-8的字节顺序标记结束.我需要删除它(用LINQ解析生成的XML),并希望在内存中这样做.
所以我有一个以\ x00EF\x00BB\x00BF开头的字符串,如果它存在,我想删除它.现在我正在使用
if (xml.StartsWith(ByteOrderMarkUtf8))
{
xml = xml.Remove(0, ByteOrderMarkUtf8.Length);
}
Run Code Online (Sandbox Code Playgroud)
但这感觉不对.我已尝试过各种带有流,GetBytes和编码的代码,但没有任何作用.任何人都可以提供"正确"算法从字符串中剥离BOM吗?
谢谢!
很多时候,我发现自己在将方法设置为私有以防止某人在无意义的上下文中调用它(或者会破坏所涉及对象的内部状态)或将该方法设为公共(或通常是内部)之间为了将它暴露给单元测试组件.我只是想知道Stack Overflow社区对这种困境的看法是什么?
所以我想真正的问题是,关注可测试性还是保持适当的封装更好?
最近我一直倾向于可测试性,因为大多数代码只会被一小群开发人员利用,但我想我会看到其他人都在想什么?
如何在Datagridview C#中为特定列设置列标题文本
我对WCF Web服务的调用失败了 System.Net.WebException: The request failed with HTTP status 413: Request Entity Too Large.
检查提琴手,我看到我正在发送:
内容长度:149839
超过65KB.
在服务器上启用WCF跟踪,我看到:
System.ServiceModel.ProtocolException:已超出传入邮件的最大邮件大小配额(65536).要增加配额,请在相应的绑定元素上使用MaxReceivedMessageSize属性.
添加此属性不能解决问题.
我已经尝试过这个属性,并且(稍后)与帖子建议的各种其他人一起尝试过.这是我目前(在服务器上):
<basicHttpBinding>
<binding name="PricerServiceSoap"
closeTimeout="00:10:00" openTimeout="00:10:00"
receiveTimeout="00:10:00" sendTimeout="00:10:00"
maxBufferSize="2147483647"
maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
maxArrayLength="2147483647" maxBytesPerRead="2147483647"
maxNameTableCharCount="2147483647" />
</binding>
</basicHttpBinding>
Run Code Online (Sandbox Code Playgroud)
我唯一的终点(下<client>)是:
<endpoint address="/NetPricingService/Service.asmx"
binding="basicHttpBinding" bindingConfiguration="PricerServiceSoap"
contract="Pricing.PricerService.PricerServiceSoap"
name="PricerServiceSoap" />
Run Code Online (Sandbox Code Playgroud)
我还补充说:
<dataContractSerializer maxItemsInObjectGraph="2147483647"/>
Run Code Online (Sandbox Code Playgroud)
在<behavior>.
我甚至跑(对于IIS 7):
%windir%\system32\inetsrv\appcmd set config "WebServicesDev/PricingService"
-section:requestFiltering -requestLimits.maxAllowedContentLength:104857600
-commitpath:apphost
Run Code Online (Sandbox Code Playgroud)
没有任何区别.
一个问题是,这是一个WCF服务,旨在取代旧的ASMX服务.服务框架是使用现有WSDL中的svcutil生成的.我无法更改客户端配置(客户端使用多种语言).我的测试客户端项目使用Add Web Reference(下Add Service Reference / Advanced)导入了该服务,因此我没有WCF配置.但是,如果我将它指向较旧的ASMX服务,则测试客户端可以正常工作.
我该如何修复或诊断?
附加信息 …
我想在一个集合中找到与另一个集合不匹配的所有项目.但是,这些集合的类型不同; 我想编写一个lambda表达式来指定相等性.
一个LINQPad我正在尝试做的例子:
void Main()
{
var employees = new[]
{
new Employee { Id = 20, Name = "Bob" },
new Employee { Id = 10, Name = "Bill" },
new Employee { Id = 30, Name = "Frank" }
};
var managers = new[]
{
new Manager { EmployeeId = 20 },
new Manager { EmployeeId = 30 }
};
var nonManagers =
from employee in employees
where !(managers.Any(x => x.EmployeeId == employee.Id))
select employee;
nonManagers.Dump(); …Run Code Online (Sandbox Code Playgroud) 在下面的示例代码中,我除以零,当我使用调试器逐步执行时,(dividend/divisor)产生无穷大或NaN(如果除数为零).当我将此结果转换为长时间时,我得到一个有效的结果,通常类似于-9223372036854775808.为什么这个演员有效?为什么不停止执行(例如抛出异常)而不是分配任意值?
double divisor = 0;
double dividend = 7;
long result = (long)(dividend / divisor);
Run Code Online (Sandbox Code Playgroud) 特定
Android.xml:
<activity android:name='.IconListActivity'
android:label='@string/icon_list_activity_name'
/>
Run Code Online (Sandbox Code Playgroud)
strings.xml中:
<string name='icon_list_activity_name>Icon List</string>
Run Code Online (Sandbox Code Playgroud)
如何在给定IconListActivity.class的情况下访问字符串'Icon List'?
我有一个有一个类库项目的解决方案,其他的是WPF应用程序.我添加了类库的引用,但是它们都不能使用它的类.
那么,问题是什么?
注意:抱歉标签,这是safari的问题.
reference .net-4.0 project-reference visual-studio-2010 visual-studio
c# ×7
.net-4.0 ×2
.net ×1
android ×1
asp.net-mvc ×1
c#-3.0 ×1
collections ×1
datagridview ×1
encoding ×1
iis ×1
linq ×1
reference ×1
string ×1
unit-testing ×1
wcf ×1
wcf-binding ×1
web-config ×1
winforms ×1