小编MrZ*_*arq的帖子

C#:从变量重新抛出异常,同时保留堆栈跟踪

在我的代码库中,我有一些重试功能,它将异常存储在变量中,最后在变量中抛出异常。想象一下这样的事情

Exception exc = null;

while(condition){
   try {
      // stuff
   } catch (Exception e){
     exc = e;
   }
}

if (e != null){
   throw e;
}
Run Code Online (Sandbox Code Playgroud)

现在,由于这是一条throw e语句而不是一条throw语句,因此原始堆栈跟踪丢失了。有没有办法进行重新抛出以保留原始堆栈跟踪,或者我需要重组我的代码以便我可以使用语句throw

.net c# exception

3
推荐指数
1
解决办法
1155
查看次数

在 C# 中将实例变量的使用限制为单个方法

我希望能够将实例变量的使用限制为一种方法,并且其他用法不应该是可能的(编译错误或警告)。例如

public class Example
{
    public string Accessor()
    {
        if (SuperPrivate == null) // allowed
        {
            SuperPrivate = "test"; // allowed
        }

        return SuperPrivate; // allowed
    }

    private string SuperPrivate;

    public void NotAllowed()
    {
        var b = SuperPrivate.Length; // access not allowed
        SuperPrivate = "wrong"; // modification not allowed
    }

    public void Allowed()
    {
        var b = Accessor().Length; // allowed
        // no setter necessary in this usecase
    }
}
Run Code Online (Sandbox Code Playgroud)

无法使用惰性、自动属性或封装在单独的对象中。我考虑过扩展 ObsoleteAttribute,但它是密封的。

c# variables methods access-control datamember

2
推荐指数
1
解决办法
317
查看次数

如何在Android Studio中使用Java创建具有自己对象的数组.

我有一段很长的代码,看起来像这样

Kwas a1 = new Kwas("Kwas Azotowy(V)", "HNO3");
// etc..
Kwas a17 = new Kwas("Kwas FluorkoWodorowy", "HF");
Run Code Online (Sandbox Code Playgroud)

如何将其写为数组?我试过类似的东西

Kwas[] a = new Kwas[17] 
Run Code Online (Sandbox Code Playgroud)

但它没有用.

我的"Kwas"课程如下所示:

public class Kwas {
String name;
String formula;

public Kwas( String nazwa, String wzor)
{
    name = nazwa;
    formula = wzor;
}

void setName(String c)
{
name = c;
}
void setFormula(String c)
{
    formula = c;
}
public String getName()
{
    return name;
}
public String  getFormula() {return formula;}
Run Code Online (Sandbox Code Playgroud)

}

java arrays oop android object

0
推荐指数
1
解决办法
1603
查看次数

标签 统计

c# ×2

.net ×1

access-control ×1

android ×1

arrays ×1

datamember ×1

exception ×1

java ×1

methods ×1

object ×1

oop ×1

variables ×1