-2

I'm trying to do something like this:

 public static T NewBinding2<T>(string pCfgName, string pCfgSuffix, string pAddr) where T : System.ServiceModel.Channels.Binding, new() { T result = null; // when pCfgSuffix is not emptry - try custom binding name... string cfgId = (!string.IsNullOrEmpty(pCfgSuffix) ? StrUtils.IncludeTrailing(pCfgName, ".") + pCfgSuffix : pCfgName); // try create binding according to config... try { result = new T(cfgId); } catch (Exception ex1) { Trace.WriteLine(TrcLvl.TraceError, TrcLvl.TraceError ? string.Format("NewBinding<{0}>.1[{1}]({2}).ERR:{3}\nat {4}", typeof(T), cfgId, pAddr, ErrorUtils.FormatErrorMsg(ex1), ErrorUtils.FormatStackTrace(ex1)) : ""); // if there was unsuccefull attempt with name suffix - then we need to try also without suffux... if (!string.IsNullOrEmpty(pCfgSuffix)) { cfgId = pCfgName; try { result = result = new T(cfgId); } catch (Exception ex2) { Trace.WriteLine(TrcLvl.TraceError, TrcLvl.TraceError ? string.Format("NewBinding<{0}>.2[{1}]({2}).ERR:{3}\nat {4}", typeof(T), cfgId, pAddr, ErrorUtils.FormatErrorMsg(ex1), ErrorUtils.FormatStackTrace(ex1)) : ""); } } if (result == null) { // in case of failure - create with default settings result = new T(); result.Name = cfgId; } } return result; } BasicHttpBinding bnd = NewBinding<BasicHttpBinding>("bndBasicHttp", "ScreensApi", "http://...."); 

But C# compiler reports me lot of errors (VS 2012, .NET 4.5):

1>WcfUtils.cs(94,28,94,40): error CS0417: 'T': cannot provide arguments when creating an instance of a variable type 1>WcfUtils.cs(104,45,104,57): error CS0417: 'T': cannot provide arguments when creating an instance of a variable type 

But why?! System.ServiceModel.Channels.Binding has the default constructor and where is specified for generics...

Is it possible to do something like that at all?

I'm just trying to optimize my code, make it shorter.

Update:

I tried to use Func<string, T> for parametrized constructor but that seems has no sense - I do not see how to use that in my case, because cfgId is variable which prepared inside a method, so what should I specify as parameter for Func<string, T> then?...

Thank you.

13
  • Just out of curiosity: Why do you use an out-param instead of returning an instance? Commented Jul 27, 2021 at 13:11
  • 1
    You have where, but you don't have new() in the list of constraints on T (as it says in the error) Commented Jul 27, 2021 at 13:16
  • 1
    BasicHttpBinding bnd = NewBinding<BasicHttpBinding>("bndBasicHttp.SE", "http://...."); this usage would point to you're using a return value, though. Commented Jul 27, 2021 at 13:20
  • 2
    I'm pretty sure that there is already question on the topic how to use parameterized ctor with generics also ... Commented Jul 27, 2021 at 13:23
  • 1
    Which IoC container are you using? Commented Jul 27, 2021 at 13:28

1 Answer 1

-2

You need to define your class as below public static void NewBinding(string pCfgName, string pAddr, out T result) where T : System.ServiceModel.Channels.Binding, new()

Sign up to request clarification or add additional context in comments.

1 Comment

This wouldn't help with new T(pCfgName, null);

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.