我有这个功能
string F(dynamic a)
{
return "Hello World!";
}
Run Code Online (Sandbox Code Playgroud)
后来当我说
dynamic a = 5;
var result = F(a);
Run Code Online (Sandbox Code Playgroud)
结果必须在编译时是字符串类型,但是没有发生,为什么?事实上,编纂通过这个
int result2 = F(a);
Run Code Online (Sandbox Code Playgroud)
而不是这个
int result3 = F(5);
Run Code Online (Sandbox Code Playgroud)
有什么帮助吗?
我正在使用网络服务。例如,当我调用某些方法时会抛出异常,因为参数是无效值。我想处理异常,但它不包含任何数据信息,只有消息“错误请求”。这是我的http响应:

try
{
var data = client.SomeMethod(4);
}
catch (Exception exception)
{
// exception.Message = Bad Request
// exception don't contains any more data information
}
Run Code Online (Sandbox Code Playgroud)
我如何捕获其他信息
我有这个方法
public static T F<T>(T arg)
{
return arg;
}
Run Code Online (Sandbox Code Playgroud)
我想为F创建一个Func委托.我试试这个
public Func<T, T> FuncF = F;
Run Code Online (Sandbox Code Playgroud)
但它在语法上并不正确.我怎么能这样做
我有一个类似这样的事件的课程
public class A
{
public event Func<string, string> Message;
public void Calling()
{
Message("Hello world!");
}
}
Run Code Online (Sandbox Code Playgroud)
如果我调用该Calling()方法并且还没有人订阅该Message事件,则它为null并抛出异常.
如何初始化我的活动?
但是,无论如何都要像数组或字典那样初始化我的类
private class A
{
private List<int> _evenList;
private List<int> _oddList;
...
}
Run Code Online (Sandbox Code Playgroud)
并说
A a = new A {1, 4, 67, 2, 4, 7, 56};
Run Code Online (Sandbox Code Playgroud)
并在我的构造函数中填充_ evenList和_ oddList及其值.
有什么办法可以访问父类的类型__注解__吗?
在上面的示例中,该类Student继承自 class Person,但它不包含该类的类型注释Person。
class Person:
name: str
address: str
def __init__(self):
print(self.__annotations__)
class Student(Person):
year: int
person = Person()
# {'name': <class 'str'>, 'address': <class 'str'>}
student = Student()
# {'year': <class 'int'>}
# HERE I would expect the name and the address props
Run Code Online (Sandbox Code Playgroud) c# ×5
.net ×4
class ×1
constructor ×1
delegates ×1
dynamic ×1
events ×1
fault ×1
func ×1
oop ×1
python ×1
python-3.x ×1
soap ×1
web-services ×1