很久以前我已经创建了一个以下模板,这样每当我执行static_cast时我都会获得一个断言,但类型不是我认为的类型:
/// perform a static_cast asserted by a dynamic_cast
template <class Type, class SourceType>
Type static_cast_checked(SourceType item)
{
Assert(!item || dynamic_cast<Type>(item));
return static_cast<Type>(item);
}
Run Code Online (Sandbox Code Playgroud)
今天我想创建一个不仅可以使用指针,还可以使用引用的变体:
/// overload for reference
template <class Type, class SourceType>
Type &static_cast_checked(SourceType &item)
{
Assert(dynamic_cast<Type *>(&item));
return static_cast<Type>(item);
}
Run Code Online (Sandbox Code Playgroud)
但是,当我转换对另一个引用的引用时,编译器似乎不使用此重载.我恐怕我不明白模板解析规则足以理解为什么,或者能够创建一个有效的变体.
注意:我无法捕获bad_cast exception
而不是检查dynamic_cast<Type *>
NULL,因为此项目禁用了异常.
这是使用LargeIcons的System.Windows.Forms.ListView的图片
所选项目显示其所有文本
例如,左上角的项目仅显示其名称的11个字符,如果选择了该字符,则会完全显示.对于未选择的项目,如何显示所有文本(或至少超过11个字符)?
我在两种智能指针之间编写了一个运算符==,并认为我应该进行快速的健全性检查.我对结果感到惊讶......
在下面的代码片段中,f和b的所有变体如何以相同的值结束?
struct Foo {
int x;
};
struct Bar : public Foo {
int y;
};
#include <iostream>
int main ()
{
Bar bar;
Foo * f = &bar;
Bar * b = &bar;
std :: cout << f << " " << b << " " << (f == b) << "\n";
void * fv = f;
void * bv = b;
std :: cout << fv << " " << bv << " " << (fv == …
Run Code Online (Sandbox Code Playgroud) 我碰到了一些奇怪的东西.DateTime
当我将它们保存到datetime
列时,SQL Server似乎不恰当地舍入了一些值.我怀疑我错过了什么,但我无法发现它.我正在使用.NET 4.0对SQL Server 2008运行此测试.以下内容应说明问题:
我在SQL Server中创建了一个名为Timestamps的表.它有两列:
id - bigint,Identity,PK
timestamp - datetime
我还创建了一个简单的测试,它执行以下操作:
Timestamps
public static void RoundTest()
{
DateTime preTruncation = DateTime.UtcNow;
DateTime truncated = preTruncation.TruncateToMilliseconds();
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["test"].ConnectionString))
{
conn.Open();
SqlCommand cmd = new SqlCommand(@"INSERT INTO Timestamps(timestamp)
VALUES(@savedTime);
SELECT SCOPE_IDENTITY() AS id");
cmd.Parameters.Add(new SqlParameter("savedTime", truncated));
cmd.Connection = conn;
var id = cmd.ExecuteScalar();
SqlCommand get = new SqlCommand(@"SELECT timestamp FROM Timestamps
WHERE id = @id");
get.Parameters.Add(new SqlParameter("id", id)); …
Run Code Online (Sandbox Code Playgroud) 我基于ASP.NET中的GridView动态生成许多不同类型的文件 - Excel电子表格和HTML文件.我正在使用此代码(这仅适用于Excel电子表格):
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=InvoiceSummary" + Request.QueryString["id"] + ".xls");
Response.Charset = "";
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
contents.RenderControl(htmlWrite);
//GridView1.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
Run Code Online (Sandbox Code Playgroud)
我想向用户提供选项,通过电子邮件将生成的文件作为附件发送给他们指定的电子邮件地址,或者将其与数据库中的帐户相关联.但我不希望用户必须保存文件,然后将其附加到表单中 - 我想自动附加生成的文件.这可能吗,它有多容易?
当然,我将使用System.Net.Mail类发送邮件......如果可能的话!
所以,假设我有一个字符串str ="MyClass",现在我想使用这个str来找到MyClass,这样我就可以实例化它,然后使用它.
例如这里:
http://msdn.microsoft.com/en-us/library/ff769550(VS.92).asp
EmailComposeTask emailComposeTask = new EmailComposeTask();
emailComposeTask.To = "user@example.com";
emailComposeTask.Body = "Email message body";
emailComposeTask.Cc = "user2@example.com";
emailComposeTask.Subject = "Email subject";
emailComposeTask.Show();
我读过它还没有用.
我想让我网站的访问者使用他们的Google帐户登录,而不必注册并创建一个新帐户.
一些东西:
我试图研究python-openid库+ google联合登录API,但我迷路了.我尽可能地了解实例化Consumer类但不了解会话并存储所需的params.我无法理解看起来如此简单的事情会如此复杂.是否真的没有一步一步的教程如何在纯python或django中做到这一点?
我试着看一下python-openid中的examples/consumer.py,但是我不理解它的500行代码.
我也不明白如何在每次向我的网站发出请求时对用户进行Google帐户验证.Google API仅说明了初始登录步骤.每次向我的网站发出必须针对谷歌服务器验证身份验证的请求会发生什么?
如果我只是在我的Web应用程序中记录异常详细信息,我是否真的需要为每个层添加异常处理逻辑?为什么不让它们全部冒出堆栈跟踪到global.asax并将它们记录在那里?
使用hibernate/jpa时,我无法在spring中自动创建表.
这是我的配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<persistence
xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="naveroTest">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>xxx</class>
...
<properties>
<property name="hibernate.archive.autodetection" value="class, hbm"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/>
<property name="hibernate.connection.url" value="jdbc:hsqldb:file:/tmp/naveroTestDB"/>
<property name="hibernate.connection.username" value="sa"/>
<property name="hibernate.connection.password" value=""/>
<property name="hibernate.hbm2ddl.auto" value="create"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
</properties>
</persistence-unit>
</persistence>
Run Code Online (Sandbox Code Playgroud)
的context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:tx="http://www.springframework.org/schema/tx"
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.4.xsd">
<!-- For auto creation of tables -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.hsqldb.jdbcDriver" />
<property name="url" value="jdbc:hsqldb:file:/tmp/naveroTestDB" />
<property name="username" value="sa" …
Run Code Online (Sandbox Code Playgroud) asp.net ×2
c# ×2
c++ ×2
java ×2
attachment ×1
casting ×1
datetime ×1
django ×1
email ×1
global-asax ×1
hibernate ×1
inheritance ×1
jpa ×1
listview ×1
logging ×1
openid ×1
pointers ×1
python ×1
reflection ×1
rounding ×1
spring ×1
sql-server ×1
templates ×1
winforms ×1