小编Ale*_*aro的帖子

动态参数的问题

我有这个功能

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)

有什么帮助吗?

.net c# dynamic

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

捕获 SOAP 错误并处理异常

我正在使用网络服务。例如,当我调用某些方法时会抛出异常,因为参数是无效值。我想处理异常,但它不包含任何数据信息,只有消息“错误请求”。这是我的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)

我如何捕获其他信息

c# soap web-services fault

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

如何创建通用Func委托

我有这个方法

    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)

但它在语法上并不正确.我怎么能这样做

.net c# delegates func

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

如何初始化事件

我有一个类似这样的事件的课程

public class A
{
    public event Func<string, string> Message;

    public void Calling()
    {
        Message("Hello world!");
    }
}
Run Code Online (Sandbox Code Playgroud)

如果我调用该Calling()方法并且还没有人订阅该Message事件,则它为null并抛出异常.

如何初始化我的活动?

.net c# events

3
推荐指数
2
解决办法
2175
查看次数

使用数组语法初始化我的类

但是,无论如何都要像数组或字典那样初始化我的类

    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及其值.

.net c# oop constructor class

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

如何访问父类的 __annotations__ ?

有什么办法可以访问父类的类型__注解__吗?

在上面的示例中,该类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)

python python-3.x python-typing

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