Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在C#中仅在经过一些操作后将值传递给基构造函数_C#_.net - Fatal编程技术网

如何在C#中仅在经过一些操作后将值传递给基构造函数

如何在C#中仅在经过一些操作后将值传递给基构造函数,c#,.net,C#,.net,好的,这个问题已经在SO中得到了回答,下面是答案 但是假设我想要操作“消息”信息,然后设置基类构造函数,那么如何操作呢 伪代码如下: public SMAPIException( string message) : base(localizedErrMessage) { TranslationHelper instance = TranslationHelper.GetTranslationHelper; // singleton class string localizedEr

好的,这个问题已经在SO中得到了回答,下面是答案

但是假设我想要操作“消息”信息,然后设置基类构造函数,那么如何操作呢

伪代码如下:

public SMAPIException( string message) : base(localizedErrMessage)
{
    TranslationHelper instance = TranslationHelper.GetTranslationHelper; // singleton class
    string localizedErrMessage = instance.GetTranslatedMessage(message, "" );
    // code removed for brevity sake.
}
//所以基本上我希望localizedErrMessage被发送到基类构造函数,而不是消息,有可能吗?请指导我。

这应该可以:

public class SMAPIException : Exception
{
    public SMAPIException(string str) : base(ChangeString(str))
    {
        /*   Since SMAPIException derives from Exceptions we can use 
         *   all public properties of Exception
         */
        Console.WriteLine(base.Message);
    }

    private static string ChangeString(string message)
    {
        return $"Exception is: \"{message}\"";
    }
}
注意
ChangeString
必须是
静态的

例如:

SMAPIException ex = new SMAPIException("Here comes a new SMAPIException");

//  OUTPUT //
// Exception is "Here comes a new SMAPIException"     
检查您的
基本类型

// Summary:
//     Initializes a new instance of the System.Exception class with a specified error
//     message.
//
// Parameters:
//   message:
//     The message that describes the error.
public Exception(string message);
调用
base(字符串消息)
newexception(“消息”)

因此,您可以使用
消息
-属性获取传递的值


但是仅当
SMAPIException
未隐藏其基本成员
新字符串消息{get;set;}
时,此选项才有效

使用静态工厂方法,并将构造函数设置为私有:

class SMAPIException
{
    private SMAPIException(string message) : base(message)
    {
        // whatever initialization
    }

    public static SMAPIException CreateNew(string message)
    {
        string localizedErrMessage;
        // do whatever to set localizedErrMessage

        return SMAPIException(localizedErrMessage);
    }
}
然后,您可以使用:

SMAPIException localizedEx = SMAPIException.CreateNew("unlocalizedString");

您可以在基类构造函数的参数列表中调用静态方法

public SMAPIException( string message) 
      : base(TranslationHelper.GetTranslationHelper.GetTranslatedMessage(message, ""))
{
}

您可以创建一个方法,该方法接受消息更改并返回该消息,然后在基本构造函数中调用它,如
:base(MakeChanges(message))
@juharr:因此在许多文件中,我需要这样做,那么我需要在我的代码中复制这个小助手方法吗?为什么不在基本构造函数中而不是子类中操作消息呢?特别是如果TranslationHelper是单例的话,“@Unbreakable”可能是重复的,只需将其作为静态类中的一个helper方法,并根据需要重用它。这样他将创建一个新的TranslationHelper 100次。@MarcoSalerno:请注意TranslationHelper是singletonclass@FelixD.:好的,我也想在我的派生类构造函数中访问相同的操纵字符串,intellisense说我有类似base.Message的东西(它与操纵字符串相同),如果没有,那么如何在派生类构造函数中访问相同的操纵字符串。请注意,我不想在派生类中再次调用helper方法。@MattRowland:您能告诉我,如何在派生类中也访问那个被操纵的字符串吗。请注意,SMAPIException派生自
Exception
Csharp类。我明白了。因此,在基类的Message属性中设置
ChangeString(str)
Message。谢谢
public SMAPIException( string message) 
      : base(TranslationHelper.GetTranslationHelper.GetTranslatedMessage(message, ""))
{
}