我正在尝试编写一种XElement以强类型方式获取值的泛型方法.这就是我所拥有的:
public static class XElementExtensions
{
public static XElement GetElement(this XElement xElement, string elementName)
{
// Calls xElement.Element(elementName) and returns that xElement (with some validation).
}
public static TElementType GetElementValue<TElementType>(this XElement xElement, string elementName)
{
XElement element = GetElement(xElement, elementName);
try
{
return (TElementType)((object) element.Value); // First attempt.
}
catch (InvalidCastException originalException)
{
string exceptionMessage = string.Format("Cannot cast element value '{0}' to type '{1}'.", element.Value,
typeof(TElementType).Name);
throw new InvalidCastException(exceptionMessage, originalException);
}
}
}
Run Code Online (Sandbox Code Playgroud)
正如你所看到First attempt的那样GetElementValue …
我试图找出如何自动注册通用抽象类或接口的实现.这是我的课程:
public abstract class AbstractValidator<T> : IValidator<T>
{
public void Validate(T)
{
...
}
}
public class CustomerValidator:AbstractValidator<Customer>
{
...
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试以下方法:
_container = new WindsorContainer();
_container.Register(
AllTypes.FromAssemblyContaining<ValidationPatterns>()
.BasedOn<IValidator>()
.WithService.Base()
}));
IValidator<Customer> val = _container.Resolve<IValidator<Customer>>();
Run Code Online (Sandbox Code Playgroud)
任何提示非常感谢.
干杯
我有以下模板struct:
template<int Degree>
struct CPowerOfTen {
enum { Value = 10 * CPowerOfTen<Degree - 1>::Value };
};
template<>
struct CPowerOfTen<0> {
enum { Value = 1 };
};
Run Code Online (Sandbox Code Playgroud)
这将是这样使用的:
const int NumberOfDecimalDigits = 5;
const int MaxRepresentableValue = CPowerOfTen<NumberOfDecimalDigits>::Value - 1;
// now can use both constants safely - they're surely in sync
Run Code Online (Sandbox Code Playgroud)
现在该模板需要Degree非负面的.我想为此强制执行编译时断言.
我怎么做?我试图添加一个析构函数CPowerOfTen:
~CPowerOfTen() {
compileTimeAssert( Degree >= 0 );
}
Run Code Online (Sandbox Code Playgroud)
但由于它没有被直接调用,因此Visual C++ 9决定不实例化它,因此根本不评估编译时断言语句.
我怎么能强制执行编译时检查Degree非负面?
我试图打开一个文件.这是我的输出(我在try/catch中构建的字符串):
打开c:\ Program Files(x86)\ MyApp\storedUsers.txt - > System.FormatException - >输入字符串格式不正确时出错.
以下是如何打开它:
installPath = @"C:\Program Files (x86)\MyApp\";
FileStream userFile = new FileStream(installPath + "storedUsers.txt", FileMode.OpenOrCreate, FileAccess.Read);
StreamReader userStream = new StreamReader(userFile);
while (userStream.Peek() >= 0)
{
string line = userStream.ReadLine();
storedUsers.Add(line.Split(',')[0], int.Parse(line.Split(',')[0]));
}
userStream.Close();
Run Code Online (Sandbox Code Playgroud)
我的抓到:
WriteToLogFile("Error opening " + installPath + "storedUsers.txt -> " + ex.GetType() + " -> " + ex.Message);
Run Code Online (Sandbox Code Playgroud)
我无法绕过什么错误...
我想拆分字符串
String fields = "name[Employee Name], employeeno[Employee No], dob[Date of Birth], joindate[Date of Joining]";
Run Code Online (Sandbox Code Playgroud)
至
name
employeeno
dob
joindate
Run Code Online (Sandbox Code Playgroud)
我为此编写了以下java代码,但它只打印名称,其他匹配不打印.
String fields = "name[Employee Name], employeeno[Employee No], dob[Date of Birth], joindate[Date of Joining]";
Pattern pattern = Pattern.compile("\\[.+\\]+?,?\\s*" );
String[] split = pattern.split(fields);
for (String string : split) {
System.out.println(string);
}
Run Code Online (Sandbox Code Playgroud)
我在这做错了什么?
谢谢
我想创建一个带图像的画廊.图库中的图像应该是可缩放的和可以整理的.我能够捏合缩放图像但无法设置缩放限制并防止图像从屏幕上平移.我使用以下代码来缩放图像:http: //code.google.com/p/4chan-image-browser/source/browse/src/se/robertfoss/MultiTouch/TouchImageView.java?specs = svnd3e623ddeb6f9e97d9eba2c7aaa7c4567a3822b5&r = d3e623ddeb6f9e97d9eba2c7aaa7c4567a3822b5
第一种方法:我使用TouchImageView向图库提供图像,这允许我捏缩放但不能滚动图库.即我无法区分"单一标签事件"和"标签滚动事件".
第二种方法:使用ImageView将图像提供给图库,如果用户点击任何图库项目,则在TouchImageView中显示所选图像,用户可以在其中捏合图像.但这也阻止我滚动图库视图.还有如何在所选图像上设置缩放限制和平移界限?
我有一个电子表格,我希望单元格公式始终查看特定单元格,即使插入行或列并且特定单元格移动.实际上,我总是希望查看表的"顶部"单元格,即使在表格顶部插入新行也是如此.
例如.单元格A2的公式为[= $ E $ 2]
现在我突出显示第1行并执行Insert Row.A2中的公式现在说[= $ E $ 3],但我希望它能看到新的第2行.
无论我对"引用"单元做什么,美元都将保留绝对的单元格引用,但无论我对"引用"单元做什么,我都希望单元格引用是绝对的.如果这是有道理的!
实际上,我在excel 2007中有一个"表",我想总是引用顶行.问题是行从顶部添加到此表中,因此顶行继续向下移动以为新的顶行腾出空间.
--- Alistair.
我正在使用C#.NET开发套接字服务器.我正在使用SocketAsyncEventArgs类提供的异步模型,因为它必须是一个高性能服务器,以便在短时间内支持许多连接.接下来,我想保护客户端和服务器之间的通信,我想我可以使用SSL.
有没有办法在SocketAsyncEventArgs模型中使用SSL?我知道.NET有SSL保护的SslStream类,但我需要使用SocketAsyncEventArgs来获得高性能.
是否可以在较高级别使用SSL,而无需在服务器代码中实现它?
提前致谢.
c# ×3
android ×2
android-ndk ×1
c++ ×1
casting ×1
excel ×1
generics ×1
java ×1
linq-to-xml ×1
opengl-es ×1
reference ×1
regex ×1
sql-server ×1
ssl ×1
templates ×1
visual-c++ ×1
xelement ×1