一个非常简单的问题,我希望得到一个"情况决定"的答案.我想知道人们对将参数传递给构造函数或方法的想法.
我会尝试为我的问题设置一个上下文:
public interface ICopier
{
void Copy();
}
public class FileCopier : ICopier
{
String m_source;
String m_destiniation;
FileCopier(String source_, String destination_)
{
m_source = source_;
m_destiniation = destiniation_;
}
public void Copy()
{
File.Copy(m_source, m_destiniation, true);
}
}
Run Code Online (Sandbox Code Playgroud)
或者FileCopier.Copy()是否应该接受source_和destination_作为方法参数?
我想让这些类尽可能抽象.
我问这个问题,因为我现在有其他接口/类用于删除,重命名等等,我想为此创建一个标准.
谢谢!
如果我有一个foreach循环,有没有办法检查一个布尔值?
我不想在内部检查foreach()然后打破例如.我想要foreach收集一个集合,同时评估某些事情是否属实.
例如,我不想这样做:
IEnumerable<Job> jobs = currentJobs;
foreach(Job job in jobs)
{
if (found)
break;
}
Run Code Online (Sandbox Code Playgroud) 我基本上创建了一个轻量级类,它传递了大约10个参数,它不会改变这些参数,它只是在构造函数中将它们存储在本地.
一些是引用类型(字符串,类),其他是值类型(int,bool,enums).
我的问题是我应该用关键字'ref'传递这些(除了我的类)吗?
我在这里考虑的是表现.
关于C#.NET中IF语句顺序的简单问题
if (Company !=null && Company.ID > 0)
{
}
Run Code Online (Sandbox Code Playgroud)
C#是否从左到右工作,因此对Company.ID的检查是否有效?
谢谢.
我正在尝试使用MVC 4中的Ajax.BeginForm帮助器方法将表单从同步转换为异步.
在我看来,我有:
@{
ViewBag.Title = "Users > Edit";
var options = new AjaxOptions()
{
Url = Url.Action("Edit", "User"),
LoadingElementId = "saving",
LoadingElementDuration = 2000,
Confirm = "Are you sure you want to save this User?"
};
}
<div id="saving" style="display:none; color:Red; font-weight: bold">
<p>Saving...</p>
</div>
@using (Ajax.BeginForm(options))
{
@Html.ValidationSummary(true)
<fieldset>
.... FIELDS ...
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
Run Code Online (Sandbox Code Playgroud)
单击提交按钮时,正在执行完整的回发.我的动作方法如下所示:
[HttpPost]
public ActionResult Edit(User user)
{
if (ModelState.IsValid)
{
_repository.Save(user);
TempData["message"] = String.Format("{0} has been saved.", user.Username); …Run Code Online (Sandbox Code Playgroud) 我使用的是.NET 2.0,因此无法访问自动属性.所以我必须采用以下编码私有变量和公共属性的方式
private string m_hello = null;
public string Hello
{
get{return m_hello;}
set{m_hello = value;}
}
Run Code Online (Sandbox Code Playgroud)
对于上述私有/公共成员的包含类的方法,是否仍然限制对私有变量的访问?我不喜欢我可以使用m_hello或Hello.
谢谢.
我一直在努力获得RESTful WCF服务,以接受JSON作为参数并返回一些JSON.
这是我的服务:
Run Code Online (Sandbox Code Playgroud)[OperationContract] [WebInvoke( Method="POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "Authenticate")] public AuthResponse Authenticate(AuthRequest data) { AuthResponse res = new AuthResponse(); if (data != null) { Debug.WriteLine(data.TokenId); res.TokenId = new Guid(data.TokenId); } return res; }
当我通过{AuthRequest:{TokenId ="some guid"}}时,上面将数据设置为null.
如果我将方法的BodyStyle设置为Bare,那么数据设置正确但我必须从JSON中删除{AuthRequest}(我真的不想这样做).有没有办法让WrappedRequests使用{AuthRequest:{TokenId ="some guid"}作为JSON?
谢谢.
ASP.NET Web API中是否有任何方法可以将异常标记为ExceptionFilterAttribute中的处理?
我想在方法级别使用异常过滤器处理异常,并停止传播到全局注册的异常过滤器.
用于控制器操作的过滤器:
public class MethodExceptionFilterAttribute : ExceptionFilterAttribute
{
public override void OnException(HttpActionExecutedContext context)
{
if (context.Exception is NotImplementedException)
{
context.Response = new HttpResponseMessage(HttpStatusCode.InternalServerError)
{
Content = new StringContent(context.Exception.Message)
};
// here in MVC you could set context.ExceptionHandled = true;
}
}
}
Run Code Online (Sandbox Code Playgroud)
全球注册的过滤器:
public class GlobalExceptionFilterAttribute : ExceptionFilterAttribute
{
public override void OnException(HttpActionExecutedContext context)
{
if (context.Exception is SomeOtherException)
{
context.Response = new HttpResponseMessage(HttpStatusCode.SomethingElse)
{
Content = new StringContent(context.Exception.Message)
};
}
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个变量:
var text = "hello";
Run Code Online (Sandbox Code Playgroud)
我想得到0个定位角色,所以:
var firstChar = text[0];
Run Code Online (Sandbox Code Playgroud)
简单.在firefox和chrome中这是有效的.在IE中然而我总是回到'undefined'
任何想法为什么会在IE中发生这种情况?
我的应用程序需要访问远程计算机上的文件,该计算机需要用户名和密码才能访问它.
我试图找出一个目录是否存在(使用Directory.Exists)来验证我是否可以建立'连接.
在使用远程目录时,有没有办法提供用户名和密码?目前Exists返回false.
干杯,
c# ×6
asp.net ×2
ajax ×1
asp.net-mvc ×1
directory ×1
filesystems ×1
foreach ×1
javascript ×1
json ×1
networking ×1
oop ×1
rest ×1
wcf ×1
web ×1
web-services ×1