gpt4 book ai didi

c# - 使用 Mono 的泛型类型参数没有装箱或类型参数转换

转载 作者:太空狗 更新时间:2023-10-29 17:58:35 27 4
gpt4 key购买 nike

下面的代码有什么问题?我看不出下面提到的错误的原因。我正在使用 Mono,这可能是 Mono 中的错误,它会在 VStudio 中编译而不会出错吗?

public static class ClientFactory {
public static T CreateClient<T, I>()
/* error here */
where T : ClientBase<I>, I
where I : class {
return CreateClient<T, I>(null, null);
}

public static T CreateClient<T, I>(string endpointConfigurationName)
/* error here */
where T : ClientBase<I>, I
where I : class {
return CreateClient<T, I>(endpointConfigurationName, null);
}

public static T CreateClient<T, I>(string endpointConfigurationName, string remoteAddress)
/* error here */
where T : ClientBase<I>, I
where I : class {
return CreateClient<T, I>(endpointConfigurationName, remoteAddress, Settings.Default.UserName, Settings.Default.Password);
}

public static T CreateClient<T, I>(string endpointConfigurationName, string remoteAddress, string userName, string password)
/* NO error here, this method compiles fine */
where T : ClientBase<I>, I
where I : class {

T client;

/* get client instance */
/* do stuff with it */

return client;
}
}

我遇到了编译错误:

…/ClientFactory.cs(14,14): Error CS0314: The type `T' cannot be used as type parameter `T' in the generic type or method `….ClientFactory.CreateClient(string, string)'. There is no boxing or type parameter conversion from `T' to `System.ServiceModel.ClientBase' (CS0314)

最佳答案

TL;DR 这可能是您的版本中的错误:它在我的 Mono 版本上完美编译。


下面的代码可以完美编译:

using System;

namespace so_test
{

public class ClientBase<T> {
// whatever
}

public static class Settings {
public static SettingValues Default;
}

public class SettingValues {
public string UserName;
public string Password;
}

public static class ClientFactory {
public static T CreateClient<T, I>()
/* error here */
where T : ClientBase<I>, I
where I : class {
return CreateClient<T, I>(null, null);
}

public static T CreateClient<T, I>(string endpointConfigurationName)
/* error here */
where T : ClientBase<I>, I
where I : class {
return CreateClient<T, I>(endpointConfigurationName, null);
}

public static T CreateClient<T, I>(string endpointConfigurationName, string remoteAddress)
/* error here */
where T : ClientBase<I>, I
where I : class {
return CreateClient<T, I>(endpointConfigurationName, remoteAddress, Settings.Default.UserName, Settings.Default.Password);
}

public static T CreateClient<T, I>(string endpointConfigurationName, string remoteAddress, string userName, string password)
/* NO error here, this method compiles fine */
where T : ClientBase<I>, I
where I : class {

T client = default(T);

/* get client instance */
/* do stuff with it */

return client;
}
}
}

imac:~ sklivvz$ mono -V
Mono JIT compiler version 2.10.6 (tarball Fri Sep 16 00:13:06 EDT 2011)
Copyright (C) 2002-2011 Novell, Inc, Xamarin, Inc and Contributors. www.mono-project.com
TLS: normal
SIGSEGV: normal
Notification: kqueue
Architecture: x86
Disabled: none
Misc: debugger softdebug
LLVM: yes(2.9svn-mono)
GC: Included Boehm (with typed GC)

关于c# - 使用 Mono 的泛型类型参数没有装箱或类型参数转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8020053/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com