Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.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# - Fatal编程技术网

C# 重写第三方中的方法

C# 重写第三方中的方法,c#,C#,我有一个第三方类,比如Log。它包含方法 public void Write(string s, params object[] args) { Monitor.Enter(this); try { string logEntry = string.Format(s, args); this.Write(logEn

我有一个第三方类,比如
Log
。它包含方法

public void Write(string s, params object[] args)
{
            Monitor.Enter(this);
            try
            {
                            string logEntry = string.Format(s, args);
                            this.Write(logEntry);
            }
            finally
            {
                            Monitor.Exit(this);
            }
}

我在自己的类中定义了一个属性
outLog

public static Log OutLog  {get;set;}
我想在.net 4.5中使用“CallerMemberNameAttribute”等功能,并重写
Write
方法,如下所示:

public void Write(string s,
    [CallerMemberName] string memberName = "",
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
    {
        Monitor.Enter(this);
        try
        {
            string logEntry = string.Format(s, memberName, sourceFilePath, sourceLineNumber);
            this.Write(logEntry);
        }
        finally
        {
            Monitor.Exit(this);
        }
    }
public static class LogExtensions
{
    public static void WriteWithCallerInfo(
        this Log log,
        string s,
        [CallerMemberName] string memberName = "",
        [CallerFilePath] string sourceFilePath = "",
        [CallerLineNumber] int sourceLineNumber = 0)
    {
        Monitor.Enter(log);
        try
        {
            string logEntry = string.Format(s, memberName, sourceFilePath, sourceLineNumber);
            log.Write(logEntry);
        }
        finally
        {
            Monitor.Exit(log);
        }
    }
}
所以我可以在课堂上称之为:

outLog.Write(...);

不确定如何覆盖?

您不能覆盖非虚拟/抽象成员


您可以这样做,但只有将实例用作类的实例时,这才有效。如果将其用作基类的实例,它将调用基类的方法。

您不能覆盖非虚拟/抽象成员


您可以这样做,但只有将实例用作类的实例时,这才有效。如果将其用作基类的实例,它将调用基类的方法。

您不能覆盖非虚拟/抽象成员


您可以这样做,但只有将实例用作类的实例时,这才有效。如果将其用作基类的实例,它将调用基类的方法。

您不能覆盖非虚拟/抽象成员


您可以这样做,但只有将实例用作类的实例时,这才有效。如果您将它用作基类的实例,它将调用基类的方法。

您不能覆盖它,但可以创建一个扩展方法(由第一个参数上的
this
关键字表示),该方法将执行您想要的操作。比如:

public void Write(string s,
    [CallerMemberName] string memberName = "",
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
    {
        Monitor.Enter(this);
        try
        {
            string logEntry = string.Format(s, memberName, sourceFilePath, sourceLineNumber);
            this.Write(logEntry);
        }
        finally
        {
            Monitor.Exit(this);
        }
    }
public static class LogExtensions
{
    public static void WriteWithCallerInfo(
        this Log log,
        string s,
        [CallerMemberName] string memberName = "",
        [CallerFilePath] string sourceFilePath = "",
        [CallerLineNumber] int sourceLineNumber = 0)
    {
        Monitor.Enter(log);
        try
        {
            string logEntry = string.Format(s, memberName, sourceFilePath, sourceLineNumber);
            log.Write(logEntry);
        }
        finally
        {
            Monitor.Exit(log);
        }
    }
}
使用此选项,您可以编写:

OutLog.WriteWithCallerInfo("whatever");
扩展方法不能只调用
Write
,因为普通方法优先于扩展方法


请注意,我也不理解锁定的原因,我认为这应该是没有必要的,假设重载
Write(int,string)
是线程安全的。

您不能重写它,但您可以创建一个扩展方法(由第一个参数上的
this
关键字表示),它将执行您想要的操作。比如:

public void Write(string s,
    [CallerMemberName] string memberName = "",
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
    {
        Monitor.Enter(this);
        try
        {
            string logEntry = string.Format(s, memberName, sourceFilePath, sourceLineNumber);
            this.Write(logEntry);
        }
        finally
        {
            Monitor.Exit(this);
        }
    }
public static class LogExtensions
{
    public static void WriteWithCallerInfo(
        this Log log,
        string s,
        [CallerMemberName] string memberName = "",
        [CallerFilePath] string sourceFilePath = "",
        [CallerLineNumber] int sourceLineNumber = 0)
    {
        Monitor.Enter(log);
        try
        {
            string logEntry = string.Format(s, memberName, sourceFilePath, sourceLineNumber);
            log.Write(logEntry);
        }
        finally
        {
            Monitor.Exit(log);
        }
    }
}
使用此选项,您可以编写:

OutLog.WriteWithCallerInfo("whatever");
扩展方法不能只调用
Write
,因为普通方法优先于扩展方法


请注意,我也不理解锁定的原因,我认为这应该是没有必要的,假设重载
Write(int,string)
是线程安全的。

您不能重写它,但您可以创建一个扩展方法(由第一个参数上的
this
关键字表示),它将执行您想要的操作。比如:

public void Write(string s,
    [CallerMemberName] string memberName = "",
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
    {
        Monitor.Enter(this);
        try
        {
            string logEntry = string.Format(s, memberName, sourceFilePath, sourceLineNumber);
            this.Write(logEntry);
        }
        finally
        {
            Monitor.Exit(this);
        }
    }
public static class LogExtensions
{
    public static void WriteWithCallerInfo(
        this Log log,
        string s,
        [CallerMemberName] string memberName = "",
        [CallerFilePath] string sourceFilePath = "",
        [CallerLineNumber] int sourceLineNumber = 0)
    {
        Monitor.Enter(log);
        try
        {
            string logEntry = string.Format(s, memberName, sourceFilePath, sourceLineNumber);
            log.Write(logEntry);
        }
        finally
        {
            Monitor.Exit(log);
        }
    }
}
使用此选项,您可以编写:

OutLog.WriteWithCallerInfo("whatever");
扩展方法不能只调用
Write
,因为普通方法优先于扩展方法


请注意,我也不理解锁定的原因,我认为这应该是没有必要的,假设重载
Write(int,string)
是线程安全的。

您不能重写它,但您可以创建一个扩展方法(由第一个参数上的
this
关键字表示),它将执行您想要的操作。比如:

public void Write(string s,
    [CallerMemberName] string memberName = "",
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
    {
        Monitor.Enter(this);
        try
        {
            string logEntry = string.Format(s, memberName, sourceFilePath, sourceLineNumber);
            this.Write(logEntry);
        }
        finally
        {
            Monitor.Exit(this);
        }
    }
public static class LogExtensions
{
    public static void WriteWithCallerInfo(
        this Log log,
        string s,
        [CallerMemberName] string memberName = "",
        [CallerFilePath] string sourceFilePath = "",
        [CallerLineNumber] int sourceLineNumber = 0)
    {
        Monitor.Enter(log);
        try
        {
            string logEntry = string.Format(s, memberName, sourceFilePath, sourceLineNumber);
            log.Write(logEntry);
        }
        finally
        {
            Monitor.Exit(log);
        }
    }
}
使用此选项,您可以编写:

OutLog.WriteWithCallerInfo("whatever");
扩展方法不能只调用
Write
,因为普通方法优先于扩展方法



请注意,我也不理解锁定的原因,我认为这应该是不必要的,假设重载
Write(int,string)
是线程安全的。

它是非虚拟的,所以您不能重写它。如果你能接受一个额外的方法,你应该在你的实现中用
base.Write
替换
this.Write
。它是非虚拟的,所以你不能重写它。如果你能接受一个额外的方法,你应该在你的实现中用
base.Write
替换
this.Write
。它是非虚拟的,所以你不能重写它。如果你能接受一个额外的方法,你应该在你的实现中用
base.Write
替换
this.Write
。它是非虚拟的,所以你不能重写它。如果你能接受一个额外的方法,你应该在你的实现中用
base.Write
替换
this.Write
。这是一个注释而不是答案。密码,什么密码?你不明白什么?你读过这篇文章吗?这是一篇评论而不是一个答案。密码,什么密码?你不明白什么?你读过这篇文章吗?这是一篇评论而不是一个答案。密码,什么密码?你不明白什么?你读过这篇文章吗?这是一篇评论而不是一个答案。密码,什么密码?你不明白什么?你看过这篇文章了吗?-@svick,
OutLog
是字段,我必须在扩展方法中传递它吗?我看到您调用
OutLog.WriteWithCallerInfo(“无论什么”)。为什么它不是
LogExtensions.WriteWithCallerInfo(“无论什么”)
@Love这就是扩展方法:您可以将它们称为
object.Method(其他参数)
,其行为与编写
StaticClassName.Method(object,其他参数)
时相同。试试看。如果你写的扩展方法看起来像实例方法,它将永远不会被使用。如果给它另一个名称,它可以是实例方法。但是,在这种情况下,我会使用扩展方法。“如果你给它另一个名称,它可以是一个实例方法。”如果你不控制原始类型,情况似乎就是这样。-@svick,
OutLog
是字段,我必须在扩展方法中传递它吗?我看到您调用
OutLog.WriteWithCallerInfo(“无论什么”)。为什么它不是
LogExtensions.WriteWithCallerInfo(“无论什么”)
@Love这就是扩展方法:您可以将它们称为
object.Method(其他参数)
,其行为与编写
s时相同