Is it possible for a class that accepts a generic type, to re-create
another instance of itself with a property type of the passed class?
ex.
public class SomeClass<T>
{
private PropertyInfo[] propInfos;
private Type objectType = typeof(T);
public SomeClass()
{
propInfos = objectType.GetProperties();
foreach (PropertyInfo pinn in propInfos)
{
if (pinn.PropertyType.IsGenericType &&
pinn.PropertyType.GetGenericTypeDefinition() == typeof(SomeClass<>))
{
//Can another SomeClass<be created here with the property's
type?
}
}
}
}
I have attacked this 10 different ways and haven't come up with
anything that works. That may be because it can't be done; has anyone
else attempted this successfully?
TIA for any help,
Terry 7 1695
On Jun 4, 5:25 pm, tadm...@yahoo.com wrote:
<snip>
I have attacked this 10 different ways and haven't come up with
anything that works. That may be because it can't be done; has anyone
else attempted this successfully?
You can certainly create closed types (and then instances of them)
with reflection. Reflection with generic types isn't much fun, but
it's definitely doable. If you could provide a short but complete
program which attempts to *use* your SomeClass<T(and verifies that
it behaves correctly) I'd be happy to have a go at implementing it.
(Unit tests would be even better than a short but complete program, of
course).
Jon
"ta*****@yahoo.com" wrote:
Is it possible for a class that accepts a generic type, to re-create
another instance of itself with a property type of the passed class?
ex.
public class SomeClass<T>
{
private PropertyInfo[] propInfos;
private Type objectType = typeof(T);
public SomeClass()
{
propInfos = objectType.GetProperties();
foreach (PropertyInfo pinn in propInfos)
{
if (pinn.PropertyType.IsGenericType &&
pinn.PropertyType.GetGenericTypeDefinition() == typeof(SomeClass<>))
{
//Can another SomeClass<be created here with the property's
type?
}
}
}
}
I have attacked this 10 different ways and haven't come up with
anything that works. That may be because it can't be done; has anyone
else attempted this successfully?
TIA for any help,
Terry
I could be wrong, but if a given type, say Foo, has a property that is a
generic declaration of SomeClass, then Foo itself would have to be generic as
well, wouldn't it?
e.g.:
class Foo<T>
{
...
public SomeClass<Tm_InnerSomeClass;
...
}
So, if you now instantiate a SomeClass object with Foo as the type:
SomeClass<FooaFooSomeClass = new SomeClass<Foo>();
Then the type is already determined. Actually instantiating this object
could potential run into issues because of the recursive nature of the type.
The only other approach would be for Foo to have already locked in the type:
class Foo
{
...
public SomeClass<intm_IntSomeClass;
...
}
And now there is no need to worry about the inner property when creating an
object of type SomeClass<Foo>.
Again, having not gotten eyeball deep in use of Generics for tricky
covariant and contravariant use cases, I could be missing something. Plus,
I'm not sure I'm really understanding your situation -- a more concrete
example of a pair of such classes with a description of what you would *want*
to happen would help.
Regards,
-- TB
On Jun 4, 12:51*pm, Thomas W. Brown
<thomas_w_br...@countrywide.NOSPAM.comwrote:
The only other approach would be for Foo to have already locked in the type:
* *class Foo
* *{
* * * ...
* * * public SomeClass<intm_IntSomeClass;
* * * ...
* *}
- Show quoted text -
Thomas,
That's basically what I've got, a property of SomeClass<>, only the
type is another class:
class Foo
{
...
public SomeClass<OtherClassTheOtherOne {}
...
}
And I haven't gotten too deep into generics either, my experience has
mostly been writing classes that inherit BindingList<Tfor grid data,
which I've done with relative success, but that's about it. ta*****@yahoo.com wrote:
Is it possible for a class that accepts a generic type, to re-create
another instance of itself with a property type of the passed class?
ex.
public class SomeClass<T>
{
private PropertyInfo[] propInfos;
private Type objectType = typeof(T);
public SomeClass()
{
propInfos = objectType.GetProperties();
foreach (PropertyInfo pinn in propInfos)
{
if (pinn.PropertyType.IsGenericType &&
pinn.PropertyType.GetGenericTypeDefinition() == typeof(SomeClass<>))
{
//Can another SomeClass<be created here with the property's
type?
}
}
}
}
Do you just want an object of the same type? Use the Activator class.
Do you need to somehow combine the type arguments from reflection with a
generic type, i.e. you found a SomeClass<Tso you now need to create a
List<T>?
Type ListT =
typeof(List).MakeGenericType(pinn.PropertyType.Get GenericArguments());
and then use Activator
On Jun 4, 1:54*pm, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
tadm...@yahoo.com wrote:
Is it possible for a class that accepts a generic type, to re-create
another instance of itself with a property type of the passed class?
ex.
public class SomeClass<T>
{
*private PropertyInfo[] propInfos;
*private Type objectType = typeof(T);
*public SomeClass()
*{
* propInfos = objectType.GetProperties();
* foreach (PropertyInfo pinn in propInfos)
* {
* * if (pinn.PropertyType.IsGenericType &&
pinn.PropertyType.GetGenericTypeDefinition() == typeof(SomeClass<>))
* * {
* * * //Can another SomeClass<be created here with the property's
type?
* * }
* }
*}
}
Do you just want an object of the same type? *Use the Activator class.
Do you need to somehow combine the type arguments from reflection with a
generic type, i.e. you found a SomeClass<Tso you now need to create a
List<T>?
Type ListT =
typeof(List).MakeGenericType(pinn.PropertyType.Get GenericArguments());
and then use Activator- Hide quoted text -
- Show quoted text -
That actually looks like it could do the trick! I researched it more
in the MSDN help, I was wondering- could a method be referenced from
an object created this way? Maybe through a delegate, or MethodInfo?
I haven't tried coding anything yet, but it's something that came to
mind as I was mulling over your suggestion.
Thanks again for the help, that could be a real life saver, I just
haven't had a chance to plug it in yet,
Terry
<ta*****@yahoo.comwrote in message
news:f8**********************************@59g2000h sb.googlegroups.com...
On Jun 4, 1:54 pm, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
>tadm...@yahoo.com wrote:
Is it possible for a class that accepts a generic type, to re-create
another instance of itself with a property type of the passed class?
ex.
public class SomeClass<T>
{
private PropertyInfo[] propInfos;
private Type objectType = typeof(T);
public SomeClass()
{
propInfos = objectType.GetProperties();
foreach (PropertyInfo pinn in propInfos)
{
if (pinn.PropertyType.IsGenericType &&
pinn.PropertyType.GetGenericTypeDefinition() == typeof(SomeClass<>))
{
//Can another SomeClass<be created here with the property's
type?
}
}
}
}
Do you just want an object of the same type? Use the Activator class.
Do you need to somehow combine the type arguments from reflection with a generic type, i.e. you found a SomeClass<Tso you now need to create a List<T>?
Type ListT = typeof(List).MakeGenericType(pinn.PropertyType.Ge tGenericArguments());
and then use Activator- Hide quoted text -
- Show quoted text -
That actually looks like it could do the trick! I researched it more
in the MSDN help, I was wondering- could a method be referenced from
an object created this way? Maybe through a delegate, or MethodInfo?
I haven't tried coding anything yet, but it's something that came to
mind as I was mulling over your suggestion.
Sure can. The best case (in terms of performance and code needed to get it
done) is when the dynamically-created object implements a known interface.
For example, List<Timplements IEnumerable no matter what T is, so you
could cast the resulting object to IEnumerable.
Otherwise, if one of the member functions is compatible with a known
delegate type, you can hook it up and call through the delegate. Creating
the delegate will be slow, the call will be very fast though, so if you call
repeatedly you will amortize the initial cost.
Calling through reflection (MethodInfo.Invoke) is the most flexible but by
far the slowest.
>
Thanks again for the help, that could be a real life saver, I just
haven't had a chance to plug it in yet,
Terry
On Jun 7, 4:31*pm, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
<tadm...@yahoo.comwrote in message
news:f8**********************************@59g2000h sb.googlegroups.com...
On Jun 4, 1:54 pm, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
tadm...@yahoo.com wrote:
Is it possible for a class that accepts a generic type, to re-create
another instance of itself with a property type of the passed class?
ex.
public class SomeClass<T>
{
private PropertyInfo[] propInfos;
private Type objectType = typeof(T);
public SomeClass()
{
propInfos = objectType.GetProperties();
foreach (PropertyInfo pinn in propInfos)
{
if (pinn.PropertyType.IsGenericType &&
pinn.PropertyType.GetGenericTypeDefinition() == typeof(SomeClass<>))
{
//Can another SomeClass<be created here with the property's
type?
}
}
}
}
Do you just want an object of the same type? Use the Activator class.
Do you need to somehow combine the type arguments from reflection with a
generic type, i.e. you found a SomeClass<Tso you now need to create a
List<T>?
Type ListT =
typeof(List).MakeGenericType(pinn.PropertyType.Get GenericArguments());
and then use Activator- Hide quoted text -
- Show quoted text -
That actually looks like it could do the trick! *I researched it more
in the MSDN help, I was wondering- could a method be referenced from
an object created this way? *Maybe through a delegate, or MethodInfo?
I haven't tried coding anything yet, but it's something that came to
mind as I was mulling over your suggestion.
Sure can. *The best case (in terms of performance and code needed to getit
done) is when the dynamically-created object implements a known interface.
For example, List<Timplements IEnumerable no matter what T is, so you
could cast the resulting object to IEnumerable.
Otherwise, if one of the member functions is compatible with a known
delegate type, you can hook it up and call through the delegate. *Creating
the delegate will be slow, the call will be very fast though, so if you call
repeatedly you will amortize the initial cost.
Calling through reflection (MethodInfo.Invoke) is the most flexible but by
far the slowest.
Thanks again for the help, that could be a real life saver, I just
haven't had a chance to plug it in yet,
Terry- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -
Ben,
This is essentially my first time of creating a custom interface, and
that worked perfectly! It's a sweet solution to be able to apply the
same functionality to completely different objects. I had it stuck in
my head that I had to use Reflection, when after all that a simple
interface with a few defined functions worked much better.
Thanks to everyone again for all the help, it has been a real life
saver. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Jasper Kent |
last post by:
I'm trying to do the equivalent of using typedefs with templates in C++ to
avoid long instantiation names.
I can do this okay:
using BigDictionary = System.Collections.Generic.Dictionary<int,...
|
by: majm |
last post by:
I'm trying to implement strongly typed lists in the 2.0 framework. I'm
using VS2005 beta 2. So far, System.Collections.Generic.List appears to
be the ideal solution. However, the...
|
by: yaniv.golan |
last post by:
Hi,
Either I am missing something, or there is a compiler bug in the way
inheritance from generic s is treated.
In the following code, class c2 inherits from c1_generic. c1_generic
has T as a...
|
by: Lloyd Dupont |
last post by:
due to cross reference I need to separate declaration and implementation.
in my header I have something like that:
--StyledRange.h--
generic <class T> ref class StyledRange;
generic <class T>...
|
by: RYoung |
last post by:
Hello all,
Given this C# generic class definition:
class Command<Key, Data>
{
BerkeleyParameter<Key> key;
BerkeleyParameter<Data> data;
public void AddParameters(BerkeleyParameter<Key>...
|
by: RFOG |
last post by:
Hi all!
I need a class that will be capable to enumerate some internal data, then
my idea is to implement "for each" loop access. Doc says that I've to inherit
form IEnumerable, but then...
|
by: raylopez99 |
last post by:
I seem to get name collision between the Generic collection SortedList
and C++.NET Framework collection SortedList.
How to resolve? Here are the libraries that seem to clash:...
|
by: Edward Diener |
last post by:
Following the example in the help for the generic List class, which
shows the Serializable attribute being used on the generic class, like so:
generic<typename T>
public ref class List :...
|
by: software |
last post by:
Hello,
I am a new bee to c++/cli, here's my question: I have a template
version of a smart pointer class I can't use in my application because
I need to use it in more than one assembly. I...
|
by: raylopez99 |
last post by:
Here is an example of a home grown generic class, representing a pair
of values. Adapted from Jon Skeet's book "C# In Depth".
The generic class is "sealed" for some reason (I think for...
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: NeoPa |
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report).
I know it can be done by selecting :...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
|
by: Teri B |
last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course.
0ne-to-many. One course many roles.
Then I created a report based on the Course form and...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
|
by: GKJR |
last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
| |