假设,我有这个界面,
interface IContact
{
IAddress address { get; set; }
}
interface IAddress
{
string city { get; set; }
}
class Person : IPerson
{
public IContact contact { get; set; }
}
class test
{
private test()
{
var person = new Person();
if (person.contact.address.city != null)
{
//this will never work if contact is itself null?
}
}
}
Run Code Online (Sandbox Code Playgroud)
Person.Contact.Address.City != null (这可以检查City是否为空.)
但是,如果Address或Contact或Person本身为null,则此检查将失败.
目前,我能想到的一个解决方案是:
if (Person != null && Person.Contact!=null && Person.Contact.Address!= null && Person.Contact.Address.City != …Run Code Online (Sandbox Code Playgroud) 我今天和我的同事进行了一次对话,她说她刚刚了解了使用该using声明背后的原因.
//Using keyword is used to clean up resources that require disposal (IDisposable interface).
using (StreamReader reader = new StreamReader(@"C:\test.txt"))
{
string line = reader.ReadLine();
}
Run Code Online (Sandbox Code Playgroud)
我指出,除非GC决定这样做,否则该物体被标记为"可以处置"但实际上没有处理和垃圾收集.
她回答说,一旦using语句结束,对象将自动处理,因为using语句被转换为try-catch-finally块.因此,对象必须放在using语句的最后.
我对此感到困惑,因为我知道使用using语句并不能保证对象被GC收集.所有发生的事情都是Dispose()调用该方法.GC无论如何都决定GC.但当她要求证明时,我找不到任何证据.
有谁知道这是如何工作的,以及如何证明它?
当WebAPI用户发生意外错误时,会看到整个堆栈跟踪.
我相信显示整个堆栈跟踪是不安全的.
停止向我的用户显示整个跟踪的默认行为是什么?
只是一个友好的信息,就像说Internal Server Error一个人就够了.正确?
有什么想法?
<?xml version="1.0"?>
<Error>
<Message>An error has occurred.</Message>
<ExceptionMessage>The method or operation is not implemented.</ExceptionMessage>
<ExceptionType>System.NotImplementedException</ExceptionType>
<StackTrace> at MyCompany.BLL.RequirementOfService.Employee1.Employee1Service.MakeRequirementOfService(RequirementOfService RequirementOfService) in d:\Projects\MyFolder\Testing\WhiteBox\MyCompany.BAL.RequirementOfService\Employee1\Employee1Service.cs:line 37
at MyCompany.BLL.RequirementOfService.RequirementOfServiceBLL.MakeRequirementOfService(RequirementOfService RequirementOfService) in d:\Projects\MyFolder\Testing\WhiteBox\MyCompany.BAL.RequirementOfService\RequirementOfServiceBLL.cs:line 76
at MyCompany.RequirementOfService.Windsor.RequirementOfServiceProvider.MakeRequirementOfService(RequirementOfService RequirementOfService) in d:\Projects\MyFolder\Testing\WhiteBox\MyCompany.RequirementOfService\Windsor\RequirementOfServiceProvider.cs:line 47
at MyCompany.RequirementOfService.RequirementOfService.Controllers.RequirementOfServiceController.Post(RequirementOfServiceDTO RequirementOfServiceDTO) in d:\Projects\MyFolder\Testing\WhiteBox\MyCompany.RequirementOfService\RequirementOfService\Controllers\RequirementOfServiceController.cs:line 87
at lambda_method(Closure , Object , Object[] )
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters)
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)
--- End of stack trace …Run Code Online (Sandbox Code Playgroud) 从重写概念开始,我重写方法Equals和GetHashCode.
主要是我想出了这个"非常简单的代码":
internal class Person
{
public string name;
public int age;
public string lname;
public Person(string name, int age, string lname)
{
this.name = name;
this.age = age;
this.lname = lname;
}
public override bool Equals(object obj)
{
var person = obj as Person;
if (person != null)
{
return person.age == this.age && person.name == this.name && person.lname == this.lname;
}
return false;
}
public override int GetHashCode()
{
return this.age.GetHashCode() * this.name.GetHashCode() * …Run Code Online (Sandbox Code Playgroud) 最近在接受采访时,我得到了这个问题.
问:您是否编写过多线程应用程序?
答:是的
问:关心解释更多?
答:我用Tasks(任务并行库)来执行一些任务waiting for some info from internet while loading UI.这提高了我的应用程序可用性.
问:但是,您是否已经使用过TPL手段来编写multithreaded应用程序?
我:(不知道该说些什么1)
那么,什么是多线程应用程序呢?它与使用不同Tasks吗?
c# multithreading .net-4.0 multitasking task-parallel-library
我所要做的就是实现观察者模式.
所以,我提出了这个解决方案:
我们有一个PoliceHeadQuarters,其主要工作是向所有订阅它的人发送通知.考虑到DSP,Inspector和SubInspector类订阅了PoliceHeadQuarters.
我写的使用事件和代表
public class HeadQuarters
{
public delegate void NewDelegate(object sender, EventArgs e);
public event EventHandler NewEvent;
public void RaiseANotification()
{
var handler = this.NewEvent;
if (handler != null)
{
handler(this, new EventArgs());
}
}
}
public class SubInspector
{
public void Listen(object sender, EventArgs e)
{
MessageBox.Show(string.Format("Event Notification received by sender = {0} with eventArguments = {1}", sender, e.ToString()));
}
}
public class Inspector
{
public void Listen(object sender, EventArgs e)
{
MessageBox.Show(string.Format("Event Notification received by sender …Run Code Online (Sandbox Code Playgroud) 我试图反序列化这个xml结构.
<?xml version="1.0"?>
<DietPlan>
<Health>
<Fruit>Test</Fruit>
<Fruit>Test</Fruit>
<Veggie>Test</Veggie>
<Veggie>Test</Veggie>
</Health>
</DietPlan>
Run Code Online (Sandbox Code Playgroud)
我试过了:
[Serializable]
[XmlRoot(ElementName = "DietPlan")]
public class TestSerialization
{
[XmlArray("Health")]
[XmlArrayItem("Fruit")]
public string[] Fruits { get; set; }
[XmlArray("Health")]
[XmlArrayItem("Veggie")]
public string[] Veggie { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
但是这引发了一个异常"XML元素已经存在于当前范围内.使用XML属性为元素指定另一个XML名称或命名空间." 谢谢你.
这是我上一次采访的方式:
问题:字符串存储在哪里?
答:堆是因为它是参考类型
问题:请解释以下代码:
static void Main(string[] args)
{
string one = "test";
string two = one;
one = one + " string";
Console.WriteLine("One is {0}" , one);
Console.WriteLine("Two is {0}", two);
}
Run Code Online (Sandbox Code Playgroud)
答案:画了两张如下图:
(代表声明, string two = one;

(代表语句, one = one + " string";.在堆上创建一个新字符串并分配)

问题:正确.为下面的代码片段绘制类似的内容:
class Program
{
static void Main(string[] args)
{
Test one = new Test { someString = "test" };
Test two = one;
one.someString = "test String";
Console.WriteLine("One …Run Code Online (Sandbox Code Playgroud) 我刚刚开始尝试使用moq对我的模块进行单元测试.
实际上,我必须编写单元测试的类使用
Assembly.GetExecutingAssembly().Location 在内部确定路径.
但是,这在编写单元测试时不起作用,因为执行程序集的路径不同(采用单元测试程序集的路径)
AppData\\Local\\Temp\\3ylnx32t.ukg\\TestApplication.Test\\assembly\\dl3\\aeb938e6\\f3664631_d982ce01.
我试过,禁用阴影复制.
AppDomainSetup appDomain= new AppDomainSetup{ShadowCopyFiles = "false",};
appDomain.ShadowCopyFiles=false.ToString();
Run Code Online (Sandbox Code Playgroud)
不过,它不起作用!
任何建议表示赞赏.提前致谢.
所以我有一个为DateTime类型实现的自定义Model Binder ,我将其注册如下:
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
GlobalConfiguration.Configuration.BindParameter(typeof(DateTime), new CurrentCultureDateTimeAPI());
}
Run Code Online (Sandbox Code Playgroud)
然后我设置了2个示例操作以查看我的自定义模型绑定是否发生:
[HttpGet]
public void BindDateTime([FromUri]DateTime datetime)
{
//http://localhost:26171/web/api/BindDateTime?datetime=09/12/2014
}
[HttpGet]
public void BindModel([FromUri]User user)
{
//http://localhost:26171/web/api/BindModel?Name=ibrahim&JoinDate=09/12/2014
}
Run Code Online (Sandbox Code Playgroud)
当我运行,并从提到的URL调用这两个动作,user的JoinDate财产被成功地使用定制绑定我配置的约束,但BindDateTime的datetime参数没有得到使用自定义粘合剂粘结.
我已经在配置中指定所有DateTime应该使用我的自定义绑定器然后为什么冷漠?建议非常感谢.
CurrentCultureDateTimeAPI.cs:
public class CurrentCultureDateTimeAPI: IModelBinder
{
public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
{
var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
var date = value.ConvertTo(typeof(DateTime), CultureInfo.CurrentCulture);
bindingContext.Model = date;
return true;
}
} …Run Code Online (Sandbox Code Playgroud) c# ×10
.net ×5
.net-4.0 ×3
.net-4.5 ×3
class ×1
dispose ×1
immutability ×1
multitasking ×1
null ×1
nunit ×1
shadow-copy ×1
stack-trace ×1
string ×1
unit-testing ×1
xml ×1