How to call generic method with stronger constraint?
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
|
show 1 more comment
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
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 ofISomeInterface
? You show usISome
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
|
show 1 more comment
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
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
c# generics constraints
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 ofISomeInterface
? You show usISome
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
|
show 1 more comment
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 ofISomeInterface
? You show usISome
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
|
show 1 more comment
3 Answers
3
active
oldest
votes
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();
}
}
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
add a comment |
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.
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
add a comment |
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>();
}
}
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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();
}
}
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
add a comment |
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();
}
}
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
add a comment |
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();
}
}
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();
}
}
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
add a comment |
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
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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>();
}
}
add a comment |
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>();
}
}
add a comment |
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>();
}
}
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>();
}
}
answered Sep 16 '10 at 16:42
Boris ModylevskyBoris Modylevsky
2,0911635
2,0911635
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 usISome
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