在 C# 中是否有更简单的方法为函数指定别名

发布于 2024-08-22 08:21:47 字数 704 浏览 3 评论 0原文

背景:在我正在移植的API中,有大量以sqlite3_为前缀的函数。它们被封装在一个名为 Sqlite3 的类中,因此函数调用是 Sqlite3.sqlite3_...

我创建了许多别名调用,类似于以下内容,

//C# alias for call    
public static void result_error_toobig(sqlite3_context pCtx)
{ sqlite3_result_error_toobig(pCtx); }

//Native call   
public static void sqlite3_result_error_toobig(sqlite3_context pCtx)
{
  Debug.Assert(sqlite3_mutex_held(pCtx.s.db.mutex));
  pCtx.isError = SQLITE_ERROR;
  setResultStrOrError(pCtx, "string or blob too big", -1,
  SQLITE_UTF8, SQLITE_STATIC);
}

这允许我编写代码,如 Sqlite3.result_error_toobig(pCtx);

问题:

  • 编译器会优化调用,从而使开销最小吗?
  • 有没有更简单的方法来创建这种类型的别名?

BACKGROUND: In the API I am porting, there are a large number of functions prefixed with sqlite3_. They are encapsulated in a class named Sqlite3, so the function call is Sqlite3.sqlite3_...

I've created a number of alias calls, similar to the following,

//C# alias for call    
public static void result_error_toobig(sqlite3_context pCtx)
{ sqlite3_result_error_toobig(pCtx); }

//Native call   
public static void sqlite3_result_error_toobig(sqlite3_context pCtx)
{
  Debug.Assert(sqlite3_mutex_held(pCtx.s.db.mutex));
  pCtx.isError = SQLITE_ERROR;
  setResultStrOrError(pCtx, "string or blob too big", -1,
  SQLITE_UTF8, SQLITE_STATIC);
}

This allows me to write code, like Sqlite3.result_error_toobig(pCtx);

QUESTION:

  • Will the compiler optimize the call, so the overhead is minimal?
  • Is there an easier way to create this type of alias?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

懒猫 2024-08-29 08:21:47

您可以使用的一种节省时间的方法是使用公共委托字段,而不是为每个函数创建方法。只要参数签名相同,你就可以这样做:

public static class Sqlite3
{
    public static Action<sqlite3_context> ResultErrorTooBig =
        sqlite3_result_error_toobig;
    public static Func<T1, T2> AnotherMethod = 
        sqlite3_another_method;
}

编辑:

如果你需要通过引用传递参数,你可能无法使用那些方便的ActionFunc 类。但是,您可以声明自己的委托类型,如下所示:

delegate int StatementDelegate(ref sqlite3_stmt pStmt);

然后,在静态 Sqlite3 类中,您可以执行以下操作:

public static StatementDelegate Finalize = sqlite3_finalize;

One time saver you could employ is using public delegate fields rather than creating methods for each function. As long as the parameter signatures are the same, you could do something like this:

public static class Sqlite3
{
    public static Action<sqlite3_context> ResultErrorTooBig =
        sqlite3_result_error_toobig;
    public static Func<T1, T2> AnotherMethod = 
        sqlite3_another_method;
}

Edit:

If you need to pass parameters by reference, you probably can't use those convenient Action and Func classes. However, you can declare your own delegate types, like this:

delegate int StatementDelegate(ref sqlite3_stmt pStmt);

Then, in your static Sqlite3 class, you could do something like this:

public static StatementDelegate Finalize = sqlite3_finalize;
谁许谁一生繁华 2024-08-29 08:21:47

编译器会优化调用,从而使开销最小吗?

是的,编译器将优化调用并有效地删除您的包装方法。编辑:在发布模式下它将得到优化,调试模式则不会。

是否有更简单的方法来创建此类别名?

根据需要创建的别名数量,您可以在 Visual Studio 中编写宏来为您完成重复性工作。只是取决于编写宏是否需要更长的时间。

PK

Will the compiler optimize the call, so the overhead is minimal?

Yes, the compiler will optimize the call and effectively remove your wrapper method. Edit: in release mode it will get optimized, debug mode will not.

Is there an easier way to create this type of alias?

Depending on the number of aliases you need to create, you could write a macro in Visual Studio to do the repetative work for you. Just depends on if it will take longer to write the macro or not.

pk

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文