How to call generic method with stronger constraint?












0















namespace Test
{
#region Not my code
public interface IAdditional
{
}
public interface ISome
{
ISomeOther<T> GetSomeother<T>() where T : class;
}
public interface ISomeOther<T> where T : class
{
void DoFoo(T obj);
}
public class AnotherClass<T> where T : class
{
}
public static class StaticClass
{
public static void DoBar<T>(AnotherClass<T> anotherClass, T obj) where T : class, IAdditional
{
}
}
#endregion

#region MyCode
public class SomeOtherImp<T> : ISomeOther<T> where T : class, IAdditional //Have to add IAdditional constraint to call StaticClass.DoBar
{
private AnotherClass<T> _anotherClass;
public void DoFoo(T obj)
{
StaticClass.DoBar<T>(_anotherClass, obj); //I do need to call StaticClass.DoBar here....
}
}
public class ISomeImp : ISome
{
public ISomeOther<T> GetSomeother<T>() where T : class
{
return new SomeOtherImp<T>(); //Can't do this no IAdditional constraint on T
}
}
#endregion
}


I was forced to add IAdditional to SomeOtherImp to be able to call StaticClass.DoBar.



And now I can't implement ISome with SomeOtherImp<T>.










share|improve this question




















  • 1





    What is the actual question?

    – Steve Townsend
    Sep 15 '10 at 14:38











  • How to call SomeStaticClass.Create<T> from ISome.Get() implemented method.

    – Alex Burtsev
    Sep 15 '10 at 14:47











  • Can you provide the code of ISomeInterface? You show us ISome only.

    – Danny Chen
    Sep 15 '10 at 14:56











  • Sorry guys i completely failed in writing sample code for my problem, here is the new edited sample code

    – Alex Burtsev
    Sep 15 '10 at 15:40






  • 3





    Don't make a new one, just fix this one.

    – Richard Hein
    Sep 15 '10 at 15:47
















0















namespace Test
{
#region Not my code
public interface IAdditional
{
}
public interface ISome
{
ISomeOther<T> GetSomeother<T>() where T : class;
}
public interface ISomeOther<T> where T : class
{
void DoFoo(T obj);
}
public class AnotherClass<T> where T : class
{
}
public static class StaticClass
{
public static void DoBar<T>(AnotherClass<T> anotherClass, T obj) where T : class, IAdditional
{
}
}
#endregion

#region MyCode
public class SomeOtherImp<T> : ISomeOther<T> where T : class, IAdditional //Have to add IAdditional constraint to call StaticClass.DoBar
{
private AnotherClass<T> _anotherClass;
public void DoFoo(T obj)
{
StaticClass.DoBar<T>(_anotherClass, obj); //I do need to call StaticClass.DoBar here....
}
}
public class ISomeImp : ISome
{
public ISomeOther<T> GetSomeother<T>() where T : class
{
return new SomeOtherImp<T>(); //Can't do this no IAdditional constraint on T
}
}
#endregion
}


I was forced to add IAdditional to SomeOtherImp to be able to call StaticClass.DoBar.



And now I can't implement ISome with SomeOtherImp<T>.










share|improve this question




















  • 1





    What is the actual question?

    – Steve Townsend
    Sep 15 '10 at 14:38











  • How to call SomeStaticClass.Create<T> from ISome.Get() implemented method.

    – Alex Burtsev
    Sep 15 '10 at 14:47











  • Can you provide the code of ISomeInterface? You show us ISome only.

    – Danny Chen
    Sep 15 '10 at 14:56











  • Sorry guys i completely failed in writing sample code for my problem, here is the new edited sample code

    – Alex Burtsev
    Sep 15 '10 at 15:40






  • 3





    Don't make a new one, just fix this one.

    – Richard Hein
    Sep 15 '10 at 15:47














0












0








0


1






namespace Test
{
#region Not my code
public interface IAdditional
{
}
public interface ISome
{
ISomeOther<T> GetSomeother<T>() where T : class;
}
public interface ISomeOther<T> where T : class
{
void DoFoo(T obj);
}
public class AnotherClass<T> where T : class
{
}
public static class StaticClass
{
public static void DoBar<T>(AnotherClass<T> anotherClass, T obj) where T : class, IAdditional
{
}
}
#endregion

#region MyCode
public class SomeOtherImp<T> : ISomeOther<T> where T : class, IAdditional //Have to add IAdditional constraint to call StaticClass.DoBar
{
private AnotherClass<T> _anotherClass;
public void DoFoo(T obj)
{
StaticClass.DoBar<T>(_anotherClass, obj); //I do need to call StaticClass.DoBar here....
}
}
public class ISomeImp : ISome
{
public ISomeOther<T> GetSomeother<T>() where T : class
{
return new SomeOtherImp<T>(); //Can't do this no IAdditional constraint on T
}
}
#endregion
}


I was forced to add IAdditional to SomeOtherImp to be able to call StaticClass.DoBar.



And now I can't implement ISome with SomeOtherImp<T>.










share|improve this question
















namespace Test
{
#region Not my code
public interface IAdditional
{
}
public interface ISome
{
ISomeOther<T> GetSomeother<T>() where T : class;
}
public interface ISomeOther<T> where T : class
{
void DoFoo(T obj);
}
public class AnotherClass<T> where T : class
{
}
public static class StaticClass
{
public static void DoBar<T>(AnotherClass<T> anotherClass, T obj) where T : class, IAdditional
{
}
}
#endregion

#region MyCode
public class SomeOtherImp<T> : ISomeOther<T> where T : class, IAdditional //Have to add IAdditional constraint to call StaticClass.DoBar
{
private AnotherClass<T> _anotherClass;
public void DoFoo(T obj)
{
StaticClass.DoBar<T>(_anotherClass, obj); //I do need to call StaticClass.DoBar here....
}
}
public class ISomeImp : ISome
{
public ISomeOther<T> GetSomeother<T>() where T : class
{
return new SomeOtherImp<T>(); //Can't do this no IAdditional constraint on T
}
}
#endregion
}


I was forced to add IAdditional to SomeOtherImp to be able to call StaticClass.DoBar.



And now I can't implement ISome with SomeOtherImp<T>.







c# generics constraints






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 9:24









BartoszKP

26.8k1067105




26.8k1067105










asked Sep 15 '10 at 14:35









Alex BurtsevAlex Burtsev

8,06054979




8,06054979








  • 1





    What is the actual question?

    – Steve Townsend
    Sep 15 '10 at 14:38











  • How to call SomeStaticClass.Create<T> from ISome.Get() implemented method.

    – Alex Burtsev
    Sep 15 '10 at 14:47











  • Can you provide the code of ISomeInterface? You show us ISome only.

    – Danny Chen
    Sep 15 '10 at 14:56











  • Sorry guys i completely failed in writing sample code for my problem, here is the new edited sample code

    – Alex Burtsev
    Sep 15 '10 at 15:40






  • 3





    Don't make a new one, just fix this one.

    – Richard Hein
    Sep 15 '10 at 15:47














  • 1





    What is the actual question?

    – Steve Townsend
    Sep 15 '10 at 14:38











  • How to call SomeStaticClass.Create<T> from ISome.Get() implemented method.

    – Alex Burtsev
    Sep 15 '10 at 14:47











  • Can you provide the code of ISomeInterface? You show us ISome only.

    – Danny Chen
    Sep 15 '10 at 14:56











  • Sorry guys i completely failed in writing sample code for my problem, here is the new edited sample code

    – Alex Burtsev
    Sep 15 '10 at 15:40






  • 3





    Don't make a new one, just fix this one.

    – Richard Hein
    Sep 15 '10 at 15:47








1




1





What is the actual question?

– Steve Townsend
Sep 15 '10 at 14:38





What is the actual question?

– Steve Townsend
Sep 15 '10 at 14:38













How to call SomeStaticClass.Create<T> from ISome.Get() implemented method.

– Alex Burtsev
Sep 15 '10 at 14:47





How to call SomeStaticClass.Create<T> from ISome.Get() implemented method.

– Alex Burtsev
Sep 15 '10 at 14:47













Can you provide the code of ISomeInterface? You show us ISome only.

– Danny Chen
Sep 15 '10 at 14:56





Can you provide the code of ISomeInterface? You show us ISome only.

– Danny Chen
Sep 15 '10 at 14:56













Sorry guys i completely failed in writing sample code for my problem, here is the new edited sample code

– Alex Burtsev
Sep 15 '10 at 15:40





Sorry guys i completely failed in writing sample code for my problem, here is the new edited sample code

– Alex Burtsev
Sep 15 '10 at 15:40




3




3





Don't make a new one, just fix this one.

– Richard Hein
Sep 15 '10 at 15:47





Don't make a new one, just fix this one.

– Richard Hein
Sep 15 '10 at 15:47












3 Answers
3






active

oldest

votes


















1














Do you mean that you want to be able to call the Get method? If you can edit the interface ISome, try this:



public interface ISome
{
T Get<T>() where T:class, ISomeInterface
}


...otherwise you're going to have to use reflection:



public class Foo : ISome
{
public T Get<T>() where T:class
{
if (!typeof(ISomeInterface).IsAssignableFrom(typeof(T))) throw new Exception();
return (T)typeof(SomeStaticClass).GetMethod("Create").MakeGenericMethod(new {typeof(T)}).Invoke();
}
}





share|improve this answer


























  • Unfortunately i can't edit ISome, SomeStaticClass

    – Alex Burtsev
    Sep 15 '10 at 14:45











  • Heh I was hoping this can be done without reflection with some inheritance magic.. looks like not.

    – Alex Burtsev
    Sep 15 '10 at 15:01



















0














You could just do this



public class Foo : ISome
{
public T Get<T>() where T : class
{
return SomeStaticClass.Create<ISomeInterface>() as T;
}
}


If it returns null, you passed in a type that was not an ISomeInterface.






share|improve this answer
























  • Looks like I have oversimplified my code actualy Get<T>() returns IBar<T> I should edit the question )

    – Alex Burtsev
    Sep 15 '10 at 15:20



















0














It looks like you are trying to implement factory design pattern. Take a look at the following piece of code. I removed the interface from restrictions of SomeClass. It compiles and will work. In my opinion ISome and its implementation Foo class are obsolete.



public static class SomeStaticClass
{
public static T Create<T>() where T:class
{
//Replace with actual construction of T
return (T)(new object());
}
}

public interface ISome
{
T Get<T>() where T : class;
}

public class Foo : ISome
{
public T Get<T>() where T:class
{
return SomeStaticClass.Create<T>();
}
}





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f3718660%2fhow-to-call-generic-method-with-stronger-constraint%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Do you mean that you want to be able to call the Get method? If you can edit the interface ISome, try this:



    public interface ISome
    {
    T Get<T>() where T:class, ISomeInterface
    }


    ...otherwise you're going to have to use reflection:



    public class Foo : ISome
    {
    public T Get<T>() where T:class
    {
    if (!typeof(ISomeInterface).IsAssignableFrom(typeof(T))) throw new Exception();
    return (T)typeof(SomeStaticClass).GetMethod("Create").MakeGenericMethod(new {typeof(T)}).Invoke();
    }
    }





    share|improve this answer


























    • Unfortunately i can't edit ISome, SomeStaticClass

      – Alex Burtsev
      Sep 15 '10 at 14:45











    • Heh I was hoping this can be done without reflection with some inheritance magic.. looks like not.

      – Alex Burtsev
      Sep 15 '10 at 15:01
















    1














    Do you mean that you want to be able to call the Get method? If you can edit the interface ISome, try this:



    public interface ISome
    {
    T Get<T>() where T:class, ISomeInterface
    }


    ...otherwise you're going to have to use reflection:



    public class Foo : ISome
    {
    public T Get<T>() where T:class
    {
    if (!typeof(ISomeInterface).IsAssignableFrom(typeof(T))) throw new Exception();
    return (T)typeof(SomeStaticClass).GetMethod("Create").MakeGenericMethod(new {typeof(T)}).Invoke();
    }
    }





    share|improve this answer


























    • Unfortunately i can't edit ISome, SomeStaticClass

      – Alex Burtsev
      Sep 15 '10 at 14:45











    • Heh I was hoping this can be done without reflection with some inheritance magic.. looks like not.

      – Alex Burtsev
      Sep 15 '10 at 15:01














    1












    1








    1







    Do you mean that you want to be able to call the Get method? If you can edit the interface ISome, try this:



    public interface ISome
    {
    T Get<T>() where T:class, ISomeInterface
    }


    ...otherwise you're going to have to use reflection:



    public class Foo : ISome
    {
    public T Get<T>() where T:class
    {
    if (!typeof(ISomeInterface).IsAssignableFrom(typeof(T))) throw new Exception();
    return (T)typeof(SomeStaticClass).GetMethod("Create").MakeGenericMethod(new {typeof(T)}).Invoke();
    }
    }





    share|improve this answer















    Do you mean that you want to be able to call the Get method? If you can edit the interface ISome, try this:



    public interface ISome
    {
    T Get<T>() where T:class, ISomeInterface
    }


    ...otherwise you're going to have to use reflection:



    public class Foo : ISome
    {
    public T Get<T>() where T:class
    {
    if (!typeof(ISomeInterface).IsAssignableFrom(typeof(T))) throw new Exception();
    return (T)typeof(SomeStaticClass).GetMethod("Create").MakeGenericMethod(new {typeof(T)}).Invoke();
    }
    }






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Sep 15 '10 at 14:52

























    answered Sep 15 '10 at 14:37









    AndrewAndrew

    324211




    324211













    • Unfortunately i can't edit ISome, SomeStaticClass

      – Alex Burtsev
      Sep 15 '10 at 14:45











    • Heh I was hoping this can be done without reflection with some inheritance magic.. looks like not.

      – Alex Burtsev
      Sep 15 '10 at 15:01



















    • Unfortunately i can't edit ISome, SomeStaticClass

      – Alex Burtsev
      Sep 15 '10 at 14:45











    • Heh I was hoping this can be done without reflection with some inheritance magic.. looks like not.

      – Alex Burtsev
      Sep 15 '10 at 15:01

















    Unfortunately i can't edit ISome, SomeStaticClass

    – Alex Burtsev
    Sep 15 '10 at 14:45





    Unfortunately i can't edit ISome, SomeStaticClass

    – Alex Burtsev
    Sep 15 '10 at 14:45













    Heh I was hoping this can be done without reflection with some inheritance magic.. looks like not.

    – Alex Burtsev
    Sep 15 '10 at 15:01





    Heh I was hoping this can be done without reflection with some inheritance magic.. looks like not.

    – Alex Burtsev
    Sep 15 '10 at 15:01













    0














    You could just do this



    public class Foo : ISome
    {
    public T Get<T>() where T : class
    {
    return SomeStaticClass.Create<ISomeInterface>() as T;
    }
    }


    If it returns null, you passed in a type that was not an ISomeInterface.






    share|improve this answer
























    • Looks like I have oversimplified my code actualy Get<T>() returns IBar<T> I should edit the question )

      – Alex Burtsev
      Sep 15 '10 at 15:20
















    0














    You could just do this



    public class Foo : ISome
    {
    public T Get<T>() where T : class
    {
    return SomeStaticClass.Create<ISomeInterface>() as T;
    }
    }


    If it returns null, you passed in a type that was not an ISomeInterface.






    share|improve this answer
























    • Looks like I have oversimplified my code actualy Get<T>() returns IBar<T> I should edit the question )

      – Alex Burtsev
      Sep 15 '10 at 15:20














    0












    0








    0







    You could just do this



    public class Foo : ISome
    {
    public T Get<T>() where T : class
    {
    return SomeStaticClass.Create<ISomeInterface>() as T;
    }
    }


    If it returns null, you passed in a type that was not an ISomeInterface.






    share|improve this answer













    You could just do this



    public class Foo : ISome
    {
    public T Get<T>() where T : class
    {
    return SomeStaticClass.Create<ISomeInterface>() as T;
    }
    }


    If it returns null, you passed in a type that was not an ISomeInterface.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Sep 15 '10 at 15:10









    kevev22kevev22

    3,5021731




    3,5021731













    • Looks like I have oversimplified my code actualy Get<T>() returns IBar<T> I should edit the question )

      – Alex Burtsev
      Sep 15 '10 at 15:20



















    • Looks like I have oversimplified my code actualy Get<T>() returns IBar<T> I should edit the question )

      – Alex Burtsev
      Sep 15 '10 at 15:20

















    Looks like I have oversimplified my code actualy Get<T>() returns IBar<T> I should edit the question )

    – Alex Burtsev
    Sep 15 '10 at 15:20





    Looks like I have oversimplified my code actualy Get<T>() returns IBar<T> I should edit the question )

    – Alex Burtsev
    Sep 15 '10 at 15:20











    0














    It looks like you are trying to implement factory design pattern. Take a look at the following piece of code. I removed the interface from restrictions of SomeClass. It compiles and will work. In my opinion ISome and its implementation Foo class are obsolete.



    public static class SomeStaticClass
    {
    public static T Create<T>() where T:class
    {
    //Replace with actual construction of T
    return (T)(new object());
    }
    }

    public interface ISome
    {
    T Get<T>() where T : class;
    }

    public class Foo : ISome
    {
    public T Get<T>() where T:class
    {
    return SomeStaticClass.Create<T>();
    }
    }





    share|improve this answer




























      0














      It looks like you are trying to implement factory design pattern. Take a look at the following piece of code. I removed the interface from restrictions of SomeClass. It compiles and will work. In my opinion ISome and its implementation Foo class are obsolete.



      public static class SomeStaticClass
      {
      public static T Create<T>() where T:class
      {
      //Replace with actual construction of T
      return (T)(new object());
      }
      }

      public interface ISome
      {
      T Get<T>() where T : class;
      }

      public class Foo : ISome
      {
      public T Get<T>() where T:class
      {
      return SomeStaticClass.Create<T>();
      }
      }





      share|improve this answer


























        0












        0








        0







        It looks like you are trying to implement factory design pattern. Take a look at the following piece of code. I removed the interface from restrictions of SomeClass. It compiles and will work. In my opinion ISome and its implementation Foo class are obsolete.



        public static class SomeStaticClass
        {
        public static T Create<T>() where T:class
        {
        //Replace with actual construction of T
        return (T)(new object());
        }
        }

        public interface ISome
        {
        T Get<T>() where T : class;
        }

        public class Foo : ISome
        {
        public T Get<T>() where T:class
        {
        return SomeStaticClass.Create<T>();
        }
        }





        share|improve this answer













        It looks like you are trying to implement factory design pattern. Take a look at the following piece of code. I removed the interface from restrictions of SomeClass. It compiles and will work. In my opinion ISome and its implementation Foo class are obsolete.



        public static class SomeStaticClass
        {
        public static T Create<T>() where T:class
        {
        //Replace with actual construction of T
        return (T)(new object());
        }
        }

        public interface ISome
        {
        T Get<T>() where T : class;
        }

        public class Foo : ISome
        {
        public T Get<T>() where T:class
        {
        return SomeStaticClass.Create<T>();
        }
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Sep 16 '10 at 16:42









        Boris ModylevskyBoris Modylevsky

        2,0911635




        2,0911635






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f3718660%2fhow-to-call-generic-method-with-stronger-constraint%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Wiesbaden

            Marschland

            Dieringhausen