<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:media="http://search.yahoo.com/mrss/" xmlns:evnet="http://www.mscommunities.com/rssmodule/"><channel><title>Comment Feed for How to work around static methods in interfaces being disallowed (TechOff on Channel 9)</title><atom:link rel="self" type="application/rss+xml" href="http://channel9.msdn.com/forums/techoff/407024-how-to-work-around-static-methods-in-interfaces-being-disallowed/rss/default.aspx" /><image><url>http://mschnlnine.vo.llnwd.net/d1/Dev/App_Themes/C9/images/feedimage.png</url><title>Comment Feed for How to work around static methods in interfaces being disallowed (TechOff on Channel 9)</title><link>http://channel9.msdn.com/forums/TechOff/407024-How-to-work-around-static-methods-in-interfaces-being-disallowed/</link></image><description>How to work around static methods in interfaces being disallowed</description><link>http://channel9.msdn.com/forums/TechOff/407024-How-to-work-around-static-methods-in-interfaces-being-disallowed/</link><language>en-us</language><pubDate>Tue, 03 Jun 2008 13:31:43 GMT</pubDate><lastBuildDate>Tue, 03 Jun 2008 13:31:43 GMT</lastBuildDate><generator>EvNet (EvNet, Version=1.0.3243.35083, Culture=neutral, PublicKeyToken=null)</generator><item><title>Re: Re: How to work around static methods in interfaces being disallowed</title><description>What if an aspect of your contract is for its implementation to possess a method for creating new instances of itself in a way that can be accessed indirectly, via a delegate or a System.Func&amp;lt;T&amp;gt;, rather than directly via the constructor?&amp;nbsp; It seems like you can't do this with an abstract class even, let alone an interface, unless I dig with System.Reflection and get a ConstructorInfo object.&amp;nbsp; And you, of all people, know why I want to avoid doing that.</description><comments></comments><link>http://channel9.msdn.com/forums/TechOff/407024-How-to-work-around-static-methods-in-interfaces-being-disallowed/?CommentID=407344</link><pubDate>Tue, 03 Jun 2008 13:14:15 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/forums/TechOff/407024-How-to-work-around-static-methods-in-interfaces-being-disallowed/?CommentID=407344</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/407344/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>What if an aspect of your contract is for its implementation to possess a method for creating new instances of itself in a way that can be accessed indirectly, via a delegate or a System.Func&amp;lt;T&amp;gt;, rather than directly via the constructor?&amp;nbsp; It seems like you can't do this with an abstract&amp;#8230;</evnet:previewtext><dc:creator>JChung2006</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/407344/Trackback.aspx</trackback:ping></item><item><title>Re: How to work around static methods in interfaces being disallowed</title><description>&lt;blockquote&gt;
				&lt;div&gt;JChung2006 wrote:&lt;/div&gt;
				&lt;div&gt;﻿&lt;blockquote&gt;&lt;table&gt;&lt;tr&gt;&lt;td&gt;&lt;img src="http://channel9.msdn.com/Themes/AlmostGlass/images/icon-quote.gif /&gt;&lt;/td&gt;&lt;td&gt;&lt;strong&gt;dcuccia wrote:&lt;/strong&gt;&lt;i&gt;﻿Yeah - I've run up against that too. The only real work-arounds I see within an interface-based context are&lt;br /&gt;
&lt;/i&gt;&lt;ul&gt;
&lt;li&gt;&lt;i&gt;create a static factory class+method that returns IDoThis objects (eg. based on an enumeration), or similarly 
&lt;/i&gt;&lt;/li&gt;&lt;li&gt;&lt;i&gt;create a static generic factory class: &lt;br /&gt;public static T CreateInstance&amp;lt;T&amp;gt;(Enum DoThisType) where T: IDoThis{...}&lt;/i&gt;&lt;/li&gt;&lt;/ul&gt;&lt;i&gt;Can't enforce private constructors though (if you're interested in a singleton).&lt;/i&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/blockquote&gt; 
&lt;p&gt;I like the second idea! In fact, you can dispose with the enum.&lt;/p&gt;&lt;pre&gt;public static class DoThisFactory {
    public static T CreateInstance&amp;lt;T&amp;gt;() where T : IDoThis, new() {
        return new T();
    }
}&lt;/pre&gt;
&lt;p&gt;Or&lt;/p&gt;&lt;pre&gt;public static class Factory&amp;lt;S&amp;gt; {
    public static T CreateInstance&amp;lt;T&amp;gt;() where T : S, new() {
        return new T();
    }
}&lt;/pre&gt;
&lt;p&gt;Thank you! :)&lt;/p&gt;&lt;/div&gt;
		&lt;/blockquote&gt;
		&lt;br /&gt;
		&lt;br /&gt;But in order to use the factory CreateInstance method you need to qualify it with the closed form expression which is the type parameter for T, but if you know what type T should be, then why not just create a new instance of it directly there?&lt;br /&gt;&lt;br /&gt;Interfaces work because I can pass around something knowing only that it fulfills a contract (e.g. IEnumerable), and can thus call the GetEnumerator() function on it regardless of the object's actual type because the compiler knows it has that function, because the object extends IEnumerator.&lt;br /&gt;&lt;br /&gt;When it comes to static calls you no longer have the object being passed around. Your only mechanism of "getting" to the method you want to call is by knowing the type itself, but then having an interface is useless to you, because you can just call the type's method statically directly instead of using the interface. That's &lt;i&gt;why &lt;/i&gt;interfaces don't have static methods.&lt;br /&gt;</description><comments></comments><link>http://channel9.msdn.com/forums/TechOff/407024-How-to-work-around-static-methods-in-interfaces-being-disallowed/?CommentID=407144</link><pubDate>Sun, 01 Jun 2008 22:14:29 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/forums/TechOff/407024-How-to-work-around-static-methods-in-interfaces-being-disallowed/?CommentID=407144</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/407144/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>	JChung2006 wrote:
				﻿dcuccia wrote:﻿Yeah - I've run up against that too. The only real work-arounds I see within an interface-based context are

create a static factory class+method that returns IDoThis objects (eg. based on an enumeration), or similarly 
create a static generic factory&amp;#8230;</evnet:previewtext><dc:creator>evildictaitor</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/407144/Trackback.aspx</trackback:ping></item><item><title>Re: How to work around static methods in interfaces being disallowed</title><description>&lt;blockquote&gt;
				&lt;div&gt;stevo_ wrote:&lt;/div&gt;
				&lt;div&gt;﻿Is this thread a joke? sorry if its not..&lt;/div&gt;
		&lt;/blockquote&gt; 
&lt;p&gt;It wasn't meant to be though looking at it again, it looks like I'm reinventing the constructor, isn't it?&amp;nbsp; Maybe this is all rather silly.&amp;nbsp; Unless I am missing something, there doesn't seem to be a way to specify constructor behavior in an interface.&lt;/p&gt;</description><comments></comments><link>http://channel9.msdn.com/forums/TechOff/407024-How-to-work-around-static-methods-in-interfaces-being-disallowed/?CommentID=407116</link><pubDate>Sun, 01 Jun 2008 22:00:34 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/forums/TechOff/407024-How-to-work-around-static-methods-in-interfaces-being-disallowed/?CommentID=407116</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/407116/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>	stevo_ wrote:
				﻿Is this thread a joke? sorry if its not..
		 
It wasn't meant to be though looking at it again, it looks like I'm reinventing the constructor, isn't it?&amp;nbsp; Maybe this is all rather silly.&amp;nbsp; Unless I am missing something, there doesn't seem to be a way to specify constructor behavior in an interface.</evnet:previewtext><dc:creator>JChung2006</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/407116/Trackback.aspx</trackback:ping></item><item><title>Re: How to work around static methods in interfaces being disallowed</title><description>&lt;blockquote&gt;
				&lt;div&gt;W3bbo wrote:&lt;/div&gt;
				&lt;div&gt;﻿ 
&lt;blockquote&gt;
&lt;table&gt;

&lt;tr&gt;
&lt;td&gt;&lt;img src="http://channel9.msdn.com/Themes/AlmostGlass/images/icon-quote.gif /&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;JChung2006 wrote:&lt;/strong&gt; 

&lt;i&gt;How would you work around this apparent C# limitation? Or is this a wrong way to think about this, and, if so, how would you approach the problem?&lt;/i&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/blockquote&gt;It's not a limitation, it's how OOP works.&lt;br /&gt;&lt;br /&gt;I recommend you read the GoF book "Design Patterns", it'll help demysterify a number of conventions and why-things-are-the-way-they-are thingies that often confuse beginners.&lt;br /&gt;&lt;br /&gt;Anyway, the "workaround" is to use the Factory pattern.&lt;br /&gt;&lt;/div&gt;
		&lt;/blockquote&gt; 
&lt;p&gt;I've read it.&amp;nbsp; I know what the Factory pattern is.&lt;br /&gt;&lt;br /&gt;P.S.&amp;nbsp; It's "demystify," not "demysterify."&lt;/p&gt;</description><comments></comments><link>http://channel9.msdn.com/forums/TechOff/407024-How-to-work-around-static-methods-in-interfaces-being-disallowed/?CommentID=407118</link><pubDate>Sun, 01 Jun 2008 21:51:39 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/forums/TechOff/407024-How-to-work-around-static-methods-in-interfaces-being-disallowed/?CommentID=407118</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/407118/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>	W3bbo wrote:
				﻿ 





JChung2006 wrote: 

How would you work around this apparent C# limitation? Or is this a wrong way to think about this, and, if so, how would you approach the problem?It's not a limitation, it's how OOP works.I recommend you read the GoF book "Design Patterns",&amp;#8230;</evnet:previewtext><dc:creator>JChung2006</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/407118/Trackback.aspx</trackback:ping></item><item><title>Re: How to work around static methods in interfaces being disallowed</title><description>&lt;blockquote&gt;
				&lt;div&gt;dcuccia wrote:&lt;/div&gt;
				&lt;div&gt;﻿Yeah - I've run up against that too. The only real work-arounds I see within an interface-based context are&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;create a static factory class+method that returns IDoThis objects (eg. based on an enumeration), or similarly 
&lt;li&gt;create a static generic factory class: &lt;br /&gt;public static T CreateInstance&amp;lt;T&amp;gt;(Enum DoThisType) where T: IDoThis{...}&lt;/li&gt;&lt;/li&gt;&lt;/ul&gt;Can't enforce private constructors though (if you're interested in a singleton).&lt;/div&gt;
		&lt;/blockquote&gt; 
&lt;p&gt;I like the second idea! In fact, you can dispose with the enum.&lt;/p&gt;&lt;pre&gt;public static class DoThisFactory {
    public static T CreateInstance&amp;lt;T&amp;gt;() where T : IDoThis, new() {
        return new T();
    }
}&lt;/pre&gt;
&lt;p&gt;Or&lt;/p&gt;&lt;pre&gt;public static class Factory&amp;lt;S&amp;gt; {
    public static T CreateInstance&amp;lt;T&amp;gt;() where T : S, new() {
        return new T();
    }
}&lt;/pre&gt;
&lt;p&gt;Thank you! :)&lt;/p&gt;</description><comments></comments><link>http://channel9.msdn.com/forums/TechOff/407024-How-to-work-around-static-methods-in-interfaces-being-disallowed/?CommentID=407132</link><pubDate>Sun, 01 Jun 2008 21:50:42 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/forums/TechOff/407024-How-to-work-around-static-methods-in-interfaces-being-disallowed/?CommentID=407132</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/407132/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>	dcuccia wrote:
				﻿Yeah - I've run up against that too. The only real work-arounds I see within an interface-based context are

create a static factory class+method that returns IDoThis objects (eg. based on an enumeration), or similarly 
create a static generic factory class: public static T&amp;#8230;</evnet:previewtext><dc:creator>JChung2006</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/407132/Trackback.aspx</trackback:ping></item><item><title>Re: How to work around static methods in interfaces being disallowed</title><description>&lt;blockquote&gt;
				&lt;div&gt;Ion Todirel wrote:&lt;/div&gt;
				&lt;div&gt;﻿how about using an abstract class? why is that method called CreateInstance, are all those classes supossed to be singletons?&lt;/div&gt;
		&lt;/blockquote&gt; 
&lt;p&gt;They weren't supposed to be singletons, just regular instances.&lt;/p&gt;
&lt;p&gt;This is not allowed:&lt;/p&gt;&lt;pre&gt;public abstract class DoThisBase {
    public abstract void DoThis();
    public abstract static DoThisBase CreateInstance();
}&lt;br /&gt;&lt;/pre&gt;
&lt;p&gt;This is also not allowed:&lt;/p&gt;&lt;pre&gt;public abstract class DoThisBase {
    public abstract void DoThis();
    public virtual static DoThisBase CreateInstance() {
        throw new NotImplementedException();
    }
}&lt;/pre&gt;
&lt;p&gt;This is allowed.&lt;/p&gt;&lt;pre&gt;public abstract class DoThisBase {
    public abstract void DoThis();
    public static DoThisBase CreateInstance() {
        throw new NotImplementedException();
    }
}&lt;/pre&gt;
&lt;p&gt;But then, to override CreateInstance in an implementation, we must hide the base class' static method with the "new" modifier.&lt;/p&gt;&lt;pre&gt;public class DoThisImpl : DoThisBase {
    public override void DoThis() {
    }
    public new static DoThisBase CreateInstance() {
        return new DoThisImpl();
    }
}&lt;/pre&gt;
&lt;p&gt;That defeats the purpose of putting the static method in the abstract base class though.&lt;/p&gt;</description><comments></comments><link>http://channel9.msdn.com/forums/TechOff/407024-How-to-work-around-static-methods-in-interfaces-being-disallowed/?CommentID=407129</link><pubDate>Sun, 01 Jun 2008 21:34:23 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/forums/TechOff/407024-How-to-work-around-static-methods-in-interfaces-being-disallowed/?CommentID=407129</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/407129/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>	Ion Todirel wrote:
				﻿how about using an abstract class? why is that method called CreateInstance, are all those classes supossed to be singletons?
		 
They weren't supposed to be singletons, just regular instances.
This is not allowed:public abstract class DoThisBase {
    public abstract&amp;#8230;</evnet:previewtext><dc:creator>JChung2006</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/407129/Trackback.aspx</trackback:ping></item><item><title>Re: How to work around static methods in interfaces being disallowed</title><description>&lt;blockquote&gt;
				&lt;div&gt;JChung2006 wrote:&lt;/div&gt;
				&lt;div&gt;How would you work around this apparent C# limitation? Or is this a wrong way to think about this, and, if so, how would you approach the problem?&lt;/div&gt;
		&lt;/blockquote&gt;
		&lt;br /&gt;
		&lt;br /&gt;It's not a limitation, it's how OOP works.&lt;br /&gt;&lt;br /&gt;I recommend you read the GoF book "Design Patterns", it'll help demysterify a number of conventions and why-things-are-the-way-they-are thingies that often confuse beginners.&lt;br /&gt;&lt;br /&gt;Anyway, the "workaround" is to use the Factory pattern.&lt;br /&gt;</description><comments></comments><link>http://channel9.msdn.com/forums/TechOff/407024-How-to-work-around-static-methods-in-interfaces-being-disallowed/?CommentID=407030</link><pubDate>Sat, 31 May 2008 23:23:33 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/forums/TechOff/407024-How-to-work-around-static-methods-in-interfaces-being-disallowed/?CommentID=407030</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/407030/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>	JChung2006 wrote:
				How would you work around this apparent C# limitation? Or is this a wrong way to think about this, and, if so, how would you approach the problem?
		
		
		It's not a limitation, it's how OOP works.I recommend you read the GoF book "Design Patterns", it'll help demysterify&amp;#8230;</evnet:previewtext><dc:creator>W3bbo</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/407030/Trackback.aspx</trackback:ping></item><item><title>Re: How to work around static methods in interfaces being disallowed</title><description>Is this thread a joke? sorry if its not..</description><comments></comments><link>http://channel9.msdn.com/forums/TechOff/407024-How-to-work-around-static-methods-in-interfaces-being-disallowed/?CommentID=407029</link><pubDate>Sat, 31 May 2008 23:14:04 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/forums/TechOff/407024-How-to-work-around-static-methods-in-interfaces-being-disallowed/?CommentID=407029</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/407029/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>Is this thread a joke? sorry if its not..</evnet:previewtext><dc:creator>stevo_</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/407029/Trackback.aspx</trackback:ping></item><item><title>Re: How to work around static methods in interfaces being disallowed</title><description>Yeah - I've run up against that too. The only real work-arounds I see within an interface-based context are&lt;br /&gt;&lt;ul&gt;&lt;li&gt;create a static factory class+method that returns IDoThis objects (eg. based on an enumeration), or similarly &lt;br /&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt;create a static generic factory class: &lt;br /&gt;public static T CreateInstance&amp;lt;T&amp;gt;(Enum DoThisType) where T: IDoThis{...}&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;Can't enforce private constructors though (if you're interested in a singleton).</description><comments></comments><link>http://channel9.msdn.com/forums/TechOff/407024-How-to-work-around-static-methods-in-interfaces-being-disallowed/?CommentID=407028</link><pubDate>Sat, 31 May 2008 22:58:00 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/forums/TechOff/407024-How-to-work-around-static-methods-in-interfaces-being-disallowed/?CommentID=407028</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/407028/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>Yeah - I've run up against that too. The only real work-arounds I see within an interface-based context arecreate a static factory class+method that returns IDoThis objects (eg. based on an enumeration), or similarly create a static generic factory class: public static T CreateInstance&amp;lt;T&amp;gt;(Enum&amp;#8230;</evnet:previewtext><dc:creator>dcuccia</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/407028/Trackback.aspx</trackback:ping></item><item><title>Re: How to work around static methods in interfaces being disallowed</title><description>how about using an abstract class? why is that method called CreateInstance, are all those classes supossed to be singletons?&lt;br /&gt;</description><comments></comments><link>http://channel9.msdn.com/forums/TechOff/407024-How-to-work-around-static-methods-in-interfaces-being-disallowed/?CommentID=407026</link><pubDate>Sat, 31 May 2008 22:38:48 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/forums/TechOff/407024-How-to-work-around-static-methods-in-interfaces-being-disallowed/?CommentID=407026</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/407026/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>how about using an abstract class? why is that method called CreateInstance, are all those classes supossed to be singletons?</evnet:previewtext><dc:creator>Ion Todirel</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/407026/Trackback.aspx</trackback:ping></item></channel></rss>