在设计类时,最好强制实现者通过使其成为方法签名的一部分来传递所有必需参数,或者创建重载并允许用户设置属性,然后检查它们是否设置正确?
例如,假设我们有一个Emailer带有方法的类Send.发送要求
public bool Send(string ToAddress, string FromAddress, string subject, string body, string Attachment = null)
Run Code Online (Sandbox Code Playgroud)
我也有公共财产
public string To { get; set; }
public string From { get; set; }
/// <summary>
/// Full filepath to attachment
/// </summary>
public string Attachment { get; set; }
public string Subject {get;set;}
public string Body {get;set;}
Run Code Online (Sandbox Code Playgroud)
现在,我应该创建一个重载Send?
public void Send()
然后编写未填充属性的检查.或者,忽略过载并可能删除属性?
是否可以在不调用命令行的情况下使用默认程序打开文件?我想运行一个单元测试并让单元测试在完成时打开文件(PDF)以进行目视检查.
我有一个问题
SELECT *
FROM table1
WHERE
documentId in
(
--select items from a second table where a third column happens to be null
select documentId from table2 t2 inner join table1 t1
on t1.documentId = t2.documentId and t1.itemId = t2.ItemId
WHERE t1.someOtherColumn is null
)
and itemId in
(
--similar query as above, just selecting itemId now
select itemId from table2 t2 inner join table1 t1
on t1.documentId = t2.documentId and t1.itemId = t2.ItemId
WHERE t1.someOtherColumn is null
)
order …Run Code Online (Sandbox Code Playgroud) 我有一个适用于我的大多数自定义类型的通用方法.今天我正在建立单元测试.扩展名失败string.string出现两个公共实例属性,Length和Chars.当我称之为GetValue炸弹"参数计数不匹配"时.
我没有任何需要允许字符串.我可以为我的通用添加一个足以解决问题的约束吗?
代码段
public static DataTable ToDataTable<T>(this List<T> items){...
//List<T> generally works...just found it failing out with string
List<string> items = new List<string> { "cookie", "apple", "whatever" };
System.Reflection.PropertyInfo[] props = typeof(string).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
foreach (var item in items)
{
var values = new object[props.Length];
for (var i = 0; i < props.Length; i++)
{
values[i] = props[i].GetValue(item, null);
}
}
Run Code Online (Sandbox Code Playgroud) 如果我有一个表格的查询
var query = from items in entityDb.TableName
join requestedBy in entityDb.People on items.RequestedById equals requestedBy.PersonId in requestedByOuter
from requestedBy in requestedByOuter.DefaultIfEmpty()
select items;
Run Code Online (Sandbox Code Playgroud)
还假设这是一个大查询,还有10个连接.然后我有一个条件过滤器
if (!string.IsNullOrEmpty(phaseFilter))
{
query = query.Where(item => item.Phase == phaseFilter);
}
Run Code Online (Sandbox Code Playgroud)
此时我相信我已经失去了延迟执行的优势.使用1000的结果可能会影响性能.考虑到我的查询实际上是50行代码,我不想去...
if(phaseFilter not null)
//50 lines....
else if(filter2 not null)
//50 lines...
else if (filter2 not null and phaseFilter not null)
//50 lines...
Run Code Online (Sandbox Code Playgroud)
啊.我可以以某种方式吃蛋糕并吃它吗?
我无法直接传递lambda
form.Invoke(() => AMethod(form));
Run Code Online (Sandbox Code Playgroud)
因为奇怪的是,lambda不是委托类型(根据编译器错误).我似乎在Jon Skeet的.NET 4.0书中回忆起了不同的信息.在MSDN上,它说:
lambda表达式是匿名函数,您可以使用来 创建委托或表达式树类型
public static void Main()
{
Form form = new Form();
form.Show();
form.Invoke(() => AMethod(form));
Application.Run();
Console.Read();
}
public static void AMethod(Form form)
{
form.SendToBack();
}
}
Run Code Online (Sandbox Code Playgroud)
然而,这里我们收到编译器错误:
无法将lambda表达式转换为类型'System.Delegate',因为它不是委托类型.
Lambda应该是匿名代表的语法糖.那么这里的故事是什么?我错过了什么?
我有一个表格,需要坐在其他两个表格之上.它也应该最小化它位于上面的两种形式之一.我发现这就像我所说的那样有效
form.Show(null);
但是,form.Show()如上所述不起作用.为什么?我意识到我将所有者作为参数传递,但所有者为null.那么为什么表单表现正常呢?
我们有一个扩展第三方应用程序的DLL.我希望这个DLL在启动纯粹用于调试目的时打开一个控制台窗口.我知道使用Windows应用程序执行此操作的技巧(将输出类型更改为控制台应用程序).有没有办法用类库做到这一点?
错误:
{"无法识别指定的类型:name ='VitalsPlugin',namespace ='',at."}
码:
public class SimpleSerializer
{
static void Main()
{
string xml = "<Plugin xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"VitalsPlugin\" ID=\"eaded5f3-7019-47b9-8f9f-e7c1879774f4\"><CopyForwardChecked>true</CopyForwardChecked></Plugin>";
StringReader reader = new StringReader(xml);
var result = Deserialize(reader);
}
static Plugin Deserialize(TextReader xml)
{
XmlSerializer xsr = new XmlSerializer(typeof(Plugin), new Type[] {typeof(VitalsPlugin)});
Plugin result = xsr.Deserialize(xml) as Plugin;
return result;
}
}
Run Code Online (Sandbox Code Playgroud)
其他有用的代码:
[XmlInclude(typeof(VitalsPlugin))]
public class Plugin
{
}
public class VitalsPlugin
{
}
Run Code Online (Sandbox Code Playgroud) 在运行时的powershell中
Get-TfsItemHistory . -Recurse -Version C13855~C14103
我收到错误 Unable to determine the source control server.
如果我运行tfpt.exe,我可以这样做tf workspaces /s:serverUrl.
我怎样才能解决这个错误?