我有一个递归方法调用.当抛出任何异常时,我想看看,在递归调用堆栈中它发生了什么.我有一个字段,其中包含一个代表递归堆栈的"路径".
现在我想将路径信息添加到可能在递归调用中抛出的任何异常.
void Recursive(int x)
{
// maintain the recursion path information
path.Push(x);
try
{
// do some stuff and recursively call the method
Recursive(x + 6);
}
catch(Exception ex)
{
if (ex is RecursionException)
{
// The exception is already wrapped
throw;
}
// wrap the exception, this should be done only once.
// save the path and original exception to the wrapper.
throw new RecursionException(path.ToString(), ex);
}
finally
{
// maintain the recursion path information
path.Pop()
}
} …Run Code Online (Sandbox Code Playgroud) 我有三节课; 类A和B两个引用类C.
我如何才能这样做,以便C在从类引用时可以修改类的成员,A但在从类引用时不进行修改B?
IE,以下应该是可能的;
classA myClassA = new classA();
myClassA.myClassC.IssueNumber = 3;
Run Code Online (Sandbox Code Playgroud)
但这不应该是可能的;
classB myClassB = new classB();
myClassB.myClassC.IssueNumber = 3;
Run Code Online (Sandbox Code Playgroud)
使classB.classC只读仍然允许classC改变属性.
我确信这是基本的东西,但找不到简单的答案.
谢谢
通过Head First Design Patterns一书的工作方式.
我相信我理解简单的工厂和工厂方法,但我很难看到工厂方法带来的简单工厂优势.
如果对象A使用简单工厂来创建其B对象,则客户端可以像这样创建它:
A a = new A(new BFactory());
Run Code Online (Sandbox Code Playgroud)
而如果一个对象使用工厂方法,客户端可以像这样创建它:
A a = new ConcreteA(); // ConcreteA contains a method for instantiating
// the same Bs that the BFactory above creates, with
// the method hardwired into the subclass of A, ConcreteA.
Run Code Online (Sandbox Code Playgroud)
因此,在简单工厂的情况下,客户端使用B工厂组成A,而使用工厂方法,客户端为其想要的B类型选择适当的子类.
他们之间似乎没有多少选择.要么你必须选择你想要用哪个BFactory,要么你必须选择A的正确子类来给你Bs.
在什么情况下哪一个比另一个好?
谢谢大家!
编辑:添加一点混乱IMO是Head First叙述中给出的解释,他们从简单的工厂转换到工厂方法,说(第119页)"特许经营权正在使用你的[简单]工厂创造比萨饼,但开始在剩下的过程中采用他们自己的本土程序:他们将事情稍微区别开来......"他们有一张厨师的照片,他显然做了一些令人厌恶的比萨饼.
但是没有什么可以使用一个简单的工厂来让客户访问bake()方法或进程的任何其他部分.并且没有任何关于使用工厂方法如果有任何问题会有所帮助.
所以在我看来,首先暗示在一家简单工厂使用工厂方法的原因是假的.
我有以下C++代码.
#include <iostream>
using namespace std;
int testfunction(int input)
{
if (input > 0) {
return 1;
}
else {
return 0;
}
}
int main()
{
testfunction(-1);
testfunction(1);
}
Run Code Online (Sandbox Code Playgroud)
我编译它来获得执行
cl /Zi hello.cpp -link /Profile
Run Code Online (Sandbox Code Playgroud)
然后,我检测执行并生成.coverage二进制文件.
vsinstr -coverage hello.exe
start vsperfmon -coverage -output:mytestrun.coverage
vsperfcmd -shutdown
Run Code Online (Sandbox Code Playgroud)
当我在VS2010中打开覆盖文件时,我的结果没有任何结果.

可能有什么问题?我按照这篇文章中的说明进行操作.
我希望通过以下测试强制执行我的代码库不可变规则
[TestFixture]
public class TestEntityIf
{
[Test]
public void IsImmutable()
{
var setterCount =
(from s in typeof (Entity).GetProperties(BindingFlags.Public | BindingFlags.Instance)
where s.CanWrite
select s)
.Count();
Assert.That(setterCount == 0, Is.True, "Immutable rule is broken");
}
}
Run Code Online (Sandbox Code Playgroud)
它通过:
public class Entity
{
private int ID1;
public int ID
{
get { return ID1; }
}
}
Run Code Online (Sandbox Code Playgroud)
但不是为了这个:
public class Entity
{
public int ID { get; private set; }
}
Run Code Online (Sandbox Code Playgroud)
这里有一个问题"WTF?"
我有两个不访问任何常见记录的事务的死锁问题.也没有锁升级.所以我无法解释为什么死锁是可能的.
当两个此类事务同时执行时发生死锁:
begin transaction
update A set [value] = [value]
where id = 1; /* resp. 2 */
/* synchronize transactions here */
SELECT *
FROM
A inner join B on A.B_FK = B.id
inner join C on C.A_FK = A.id
WHERE
A.[value] = 1; /* resp. 2 */
rollback;
Run Code Online (Sandbox Code Playgroud)
这些是用于设置场景的表和数据:
CREATE TABLE A (
id INT NOT NULL,
[value] INT,
B_FK INT
primary key (id)
)
CREATE TABLE B (
id INT NOT NULL,
primary key (id)
)
CREATE …Run Code Online (Sandbox Code Playgroud) 我需要一个伪随机生成器,它将一个数字作为输入并返回另一个数字,它是可重现的并且似乎是随机的.
它一定不是完美的,它只是创建随机但可重现的测试数据.
我用C#.
我前段时间写了这段有趣的代码,它产生了随机的东西.
public static long Scramble(long number, long max)
{
// some random values
long[] scramblers = { 3, 5, 7, 31, 343, 2348, 89897 };
number += (max / 7) + 6;
number %= max;
// shuffle according to divisibility
foreach (long scrambler in scramblers)
{
if (scrambler >= max / 3) break;
number = ((number * scrambler) % max)
+ ((number * scrambler) / max);
}
return number % …Run Code Online (Sandbox Code Playgroud) 我有一个程序在SQL Server 2005或2008数据库上执行许多批量操作(删除和创建索引,创建列,全表更新等),所有这些都在一个事务中完成.
有什么问题可以预料吗?
还有其他原因可以将交易分成更小的步骤吗?
花了一段时间才找到答案,以为我会分享爱情.
当使用NHibernate通过代码与SQL Server的新映射时,我无法保存实体.保存实体时,将抛出System.Data.SqlClient.SqlException并显示以下消息(减去表名):
"当IDENTITY_INSERT设置为OFF时,无法在表'DietUser'中为标识列插入显式值."
我的表使用身份ID,实体和映射看起来像这样:
public class User
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual string Username { get; set; }
public virtual string Password { get; set; }
public virtual short DailyPoints { get; set; }
}
public class UserMapping : ClassMapping<User>
{
public UserMapping()
{
Id(x => x.Id);
Property(x => x.Name);
Property(x => x.Username);
Property(x => x.Password);
Property(x => x.DailyPoints);
}
}
Run Code Online (Sandbox Code Playgroud)
我知道如何使用XML映射来映射它,但我希望尽可能使用代码内置的映射.
sql-server nhibernate nhibernate-mapping nhibernate-mapping-by-code
我是泛型的新手,并在文章“参数化类型,例如ArrayList<T>,不可实例化 - 我们无法创建它们的实例”中读到过。
完整引用,来自 Java in a Nutshell:
参数化类型,例如
ArrayList<T>,不可实例化 - 我们无法创建它们的实例。这是因为<T>只是一个类型参数 - 只是一个真正类型的占位符。只有当我们为类型参数提供一个具体的值时,(例如,ArrayList<String>),类型才完全成形,我们可以创建该类型的对象。如果我们要使用的类型在编译时未知,这就会带来问题。幸运的是,Java 类型系统能够适应这个概念。它通过具有表示为 的未知类型的显式概念来实现
<?>。
我知道它不应该是可实例化的,因为具体(实际)类型未知。如果是这样,为什么下面的代码编译没有错误?
public class SampleTest {
public static <T> List<T> getList(T... elements) {
List<T> lst = new ArrayList<>(); // shouldn't this line return an error?
return lst;
}
}
Run Code Online (Sandbox Code Playgroud)
我知道我在这里对泛型的理解存在差距。有人可以指出我在这里缺少什么吗?
c# ×4
sql-server ×2
bulk ×1
c++ ×1
deadlock ×1
generics ×1
immutability ×1
java ×1
nhibernate ×1
oop ×1
profiling ×1
properties ×1
random ×1
readonly ×1
recursion ×1
reflection ×1
test-data ×1
transactions ×1