-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.

dmitry_bond
  • 378
  • 1
  • 3
  • 15
  • Just out of curiosity: Why do you use an out-param instead of returning an instance? – Fildor Jul 27 '21 at 13:11
  • The problem is: the compiler cannot kow if there _is_ a constructor with those parameters. What you could do, is add the new() constraint and use property setters instead of ctor-params. But I guess you prefer ctor params... – Fildor Jul 27 '21 at 13:13
  • 1
    You have `where`, but you don't have `new()` in the list of constraints on `T` (as it says in the error) – Flydog57 Jul 27 '21 at 13:16
  • The constraint `where T : System.ServiceModel.Channels.Binding` would allow `T` to be a _subtype_ of `System.ServiceModel.Channels.Binding` which may define another constructor. – Johnathan Barclay Jul 27 '21 at 13:18
  • I tried return type but that reported more errors, so changed to our-param... – dmitry_bond Jul 27 '21 at 13:18
  • @Selvin Mind, there is a parameterized ctor in use here. – Fildor Jul 27 '21 at 13:18
  • And, once you get the `new()` constraint in place, you will find out (as @Fildor has pointed out), that it only works for default constructors. You can using `new T(some parameters)` – Flydog57 Jul 27 '21 at 13:18
  • 1
    `BasicHttpBinding bnd = NewBinding("bndBasicHttp.SE", "http://....");` this usage would point to you're using a return value, though. – Fildor Jul 27 '21 at 13:20
  • @Selvin: Ah missed, you added a dupe. Nevermind. – Fildor Jul 27 '21 at 13:21
  • 2
    I'm pretty sure that there is already question on the topic how to use parameterized ctor with generics also ... – Selvin Jul 27 '21 at 13:23
  • @Selvin But the solution doesn't apply here. There, it was a duplicate of Type-Parameter and a class name. Creation then was based on the (non-generic) class and its ctor. – Fildor Jul 27 '21 at 13:24
  • 1
    Which IoC container are you using? – mjwills Jul 27 '21 at 13:28
  • Not sure about "IoC container". This is Windows service app with custom code in Program.cs which allows to run this app as console application (when certain cmdline prm specified). It is not IIS, not Web. This console app. 1 service app which connects 2 WCF servers +1 more console app which is remote control for 1st console app (also via WCF). Currently no duplex communications used for WCF. – dmitry_bond Jul 27 '21 at 16:34

1 Answers1

-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()