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.