加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 使用扩展方法而不调用类名

发布时间:2020-12-15 23:30:33 所属栏目:百科 来源:网络整理
导读:我正在尝试创建一个静态(全局)函数,我可以使用扩展方法在项目的任何脚本中调用,但我不认为我正在实现它. 文件:extensions.cs namespace CustomExtensions{ public static class MathExt { public static float Remap (float value,float from1,float to1,fl
我正在尝试创建一个静态(全局)函数,我可以使用扩展方法在项目的任何脚本中调用,但我不认为我正在实现它.

文件:extensions.cs

namespace CustomExtensions
{
  public static class MathExt
  {
    public static float Remap (float value,float from1,float to1,float from2,float to2)
    {
      return (((value - from1) * (to2 - from2) / (to1 - from1)) + from2);
    }
  }
}

现在从另一个文件中我希望能够使用这种语法:

using CustomExtensions;

public class MySound
{
  public void SetPitch(float rpm)
  {
    pitch = Remap(rpm,minRPM,maxRPM,0.5f,1.5f);
  }
}

但是我得到一个错误,除非我做MathExt.Remap(rpm,720,.75f,1.75f);

我也尝试过使用CustomExtensions.MathExt;但它仍然抛出一个错误.

我想调用此函数而不必在它之前声明MathExt.我意识到只添加类名很简单,但我想了解我做错了什么.

解决方法

这不是一种扩展方法.您没有定义作为扩展方法基础的对象(您使用此方法执行此操作):

public static float Remap (this float value,float to2)
{ }

然后你称之为:

pitch = rpm.Remap(minRPM,1.5f);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读