小编Ali*_*ili的帖子

C#接口的方法的默认实现?

是否可以在C#中定义具有默认实现的接口?(这样我们就可以定义一个实现该接口的类,而无需实现该特定的默认方法).

我知道扩展方法(例如在此链接中解释).但这不是我的答案,因为有一个像下面的方法扩展,编译器仍然抱怨在MyClass中实现MyMethod:

public interface IMyInterface
{
    string MyMethod();
}

public static class IMyInterfaceExtens
{
    public static string MyMethod(this IMyInterface someObj)
    {
        return "Default method!";
    }
}

public class MyClass: IMyInterface
{
// I want to have a default implementation of "MyMethod" 
// so that I can skip implementing it here
}
Run Code Online (Sandbox Code Playgroud)

我问这个是因为(至少据我所知)可以用Java实现(见这里).

PS:拥有一个带有某种方法的抽象基类也不是我的答案,因为我们在C#中没有多重继承,它与接口的默认实现(如果可能!)不同.

c# oop interface

31
推荐指数
5
解决办法
4万
查看次数

Telegram bot 通过 URL 发送照片返回“错误请求:指定了错误的文件标识符/HTTP URL”

我在Telegram bot 中通过 url发送照片。对于某些照片,我收到来自 Telegram 的错误消息:

{"ok":false,"error_code":400,"description":"Bad Request: wrong file identifier/HTTP URL specified"}
Run Code Online (Sandbox Code Playgroud)

例如:发送这张照片作品:

https://api.telegram.org/bot<BOT_KEY>/sendPhoto?chat_id=<CHAT_ID>&photo=https%3A%2F%2Fdrscdn.500px.org%2Fphoto%2F153590277%2Fq%253D80_m%253D2000%2Fv2%3Fwebp%3Dtrue%26sig%3D8b429a27872dfdb4f68ddc5edd488ce9e6a57977415fa323178cd62c5100a3ff
Run Code Online (Sandbox Code Playgroud)

但这个文件奇怪地不起作用:

https://api.telegram.org/bot<BOT_KEY>/sendPhoto?chat_id=<CHAT_ID>&photo=https%3A%2F%2Fdrscdn.500px.org%2Fphoto%2F247611167%2Fq%253D80_m%253D1500%2Fv2%3Fwebp%3Dtrue%26sig%3Dcfa117f225962250323c1202797abe8d45b47d59da12d780f4bf5231687c4331
Run Code Online (Sandbox Code Playgroud)

请注意,对于这两个示例:

  • 网址有效
  • MIME 类型似乎没问题(两者实际上都是 jpg 文件)
  • 文件大小小于 10 MB

难道我做错了什么?或者对这个问题有什么想法?

谢谢,

telegram-bot

4
推荐指数
2
解决办法
4914
查看次数

在C#中调用具有未知类型和参数数量的函数

我正在用C#编写一个库,我必须在其类中调用其他程序员定义的一些方法,所以我不了解类型和参数数量.例如,

Class exampleClass{
    void method1(int param1, double param2){...}
    bool method2(){...}
    object method3(string param1){....}
}
Run Code Online (Sandbox Code Playgroud)

在我的程序中,我想调用这些方法.由于我不知道他们的参数和返回类型,我不能使用" 委托 'S(具有已知类型和参数),但在运行时,我可以使用,例如,反射提取方法和参数(’ MethodInfo的 "s)来自班级,但如何使用这些信息来调用方法?(假设我可以生成适当的值以用作方法的参数).

谢谢

PS:我知道"params object []"方法,但它会强制程序员使用"params"对象,而不是在他们的方法中定义它们的常用参数.所以,我不想使用这种方法.

c# delegates

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

如何在c#中提取对象类型中数组的元素?

我有一个对象,其中包含任何类型的数组(不是已知的类型.所以我不能在代码中进行简单的转换,因为我可以在运行时确定类型!).如何提取数组的内容?例如:

int[] array = new int[] { 0, 1, 2 };
object obj=array;
...
//Here I know that obj is an array (<b>of a priory unknown type and I cannot use type conversion in the code </b> 
//How can extract elements of obj and use them, e.g. write them on the screen?`
Run Code Online (Sandbox Code Playgroud)

c# arrays object

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

标签 统计

c# ×3

arrays ×1

delegates ×1

interface ×1

object ×1

oop ×1

telegram-bot ×1