I guess that you want to return a collection of a specific kind of Entity. So you can first define a function with two parameters:
1) T entity——entity model class.
2) object collection——for containing vlaues.
public void AutoGetValues<T>(T entity,object[]values) where T:TEntity { var results = entity.GetProperties(); values = new object[results.Length]; int i=0; foreach (PropertyInfo p in results) { values[i++] = p.GetValue(entity,null); } }
3) And then call this method as: object[] values = null; AutoGetValues<ModelClass>(ModelClassInstance, values);
Couldn´t make it work. Would you mind indicating me how to use with real life data like Northwind, Products, etc. I don´t know what to write in every step of the way. I have tried different combinations but still no luck
It seems that it requires something else that you already have taht I don´t as to work fine
I just copied and pasted the code in my OnInserting FormView event but it gives a lot of error messages
Please
----------------------
Decker Dong - MSFT
Hello topolov,
I guess that you want to return a collection of a specific kind of Entity. So you can first define a function with two parameters:
1) T entity——entity model class.
2) object collection——for containing vlaues.
public void AutoGetValues<T>(T entity,object[]values) where T:TEntity { var results = entity.GetProperties(); values = new object[results.Length]; int i=0; foreach (PropertyInfo p in results) { values[i++] = p.GetValue(entity,null); } }
3) And then call this method as: object[] values = null; AutoGetValues<ModelClass>(ModelClassInstance, values);
Sorry for asking but it is just because I don't understand
I'm not a sloppy person but I just don't get it
But it's ok. That had happened to me a lot here in this forum. From where I come we say that it's much bertter to ask twice than look like a fool the rest of your life so it seems natural for us asking when we reach a dead end road
If I were supposed to know as much as I should on the .ASP. Net Dynamic Data framework then I wouldn't come to ask here
Problem is that Dynamic Data literature is not really available from the manufacturer and to be true all the help environment of the official web site lacks of true examples and power
You must go somehwre else to find the answers
I have asked on this several times with no luck
How difficult can taht be to convert the word "Products" into something which .Net understands as the "Products" entity instead
There are many languages where this is done with no major problem at all but in .Net thins must go hand by hand with Intellisense and there you have the problem: not knowing what to do to make a simple substitution
I must tell you that I was laid off 2 years ago and that I´m not in a good situation, financially speaking, now
That said, I must also explain you that I come to a cyber coffee to do my research. I have cut off my Internet services, cable, phone and yesterday I was also cut my electrical supply
Regarding what I´m doing to make a living is this: I´m trying to write a distributable invoicing application based on Dynamic Data. I started writing it almost a year ago. Haven´t finished yet because tehre are so many things regarding Dynamic data that
are not really available for us anywhere else on the web but here and some blogs of some of your contributors
So I'll be glad to submit what you've asked but I have to prepare it and upload it to you which means anoyher day that I must wait
Having said all of that: I don´t understand what does my design have to do whit a simple question. All I need to know is this:
I have a string "Products". I also have other strings: names "as simple text" of the other NorthWind entities: "Suppliers", "Orders", etc.
I also have the string of the name of the DataModel which is declared in the global.asax file
All of them as strings
I need a way of "converting" those strings in something which might be usable when I pass them as parameteres for calling a method
I need a way of achieving "instantiating" those strings so I can have access to the properties of the entities they represent
Like:
var comingentity = table.Name;
(of which I got the string "Products")
[some kind of conversion here ??? ]
-- I don´t know what to do here to convert the string "Products" into the entity class Products
comingentity myentity = new comingentity();
which must act like if I were writing
Products myproducts = new Products();
and then I could use all of myproducts properties as If I had instantiated Products
This is silly but if you have 100 entities you wouldn´t like to create switch statements for instantiating a 100 different entities, would you like that?
So, as simple as that: How to convert a string which represents an entity name into the entity itself?
I don´t know what to do here to convert the string "Products" into the entity class Products
Hello Topolov:)
Don't worry and take it easy:)
Since you are using EntityFramework and Products should be a public property of that... You can have a try like this:
using (DbEntites dbe = new DbEntities)
{
dynamic products = dbe.GetType().GetProperty("Products").GetConstructor(Type.EmptyTypes).Invoke(null); //Here you can use
GetConstructor method and Invoke to generate a new products list. And then do what you like...
}
Ok, late answer from my side buty as I said I'm currently cybering as to reach the net
Not really lucky these days with money
I'm obligued to "unmark" the non-answer answer from Mr. Dong but his code doesn't work as expected
It doesn't work. GetConstructor is not available under taht type of construction
I've seen a previosu answer from him in which it works but then he uses Type at the begining of the line isntead of dbe whcih makes a huge difference in what are you doing and getting
Topolov
Member
680 Points
411 Posts
Reflection (About)
Oct 01, 2011 06:53 PM|LINK
What if I have an entity instantiated as: Products products = new Products();
what if I had 30 more entities and I´d like to pass to the constructort a variable that can be recognized as an entity like:
var comingentity = table.Name;
[some kind of conversion here ??? ]
comingentity myentity = new comingentity();
sjnaughton
All-Star
27393 Points
5486 Posts
MVP
Re: Reflection (About)
Oct 02, 2011 01:38 PM|LINK
Hi Topolov, not sure what you are tryign to achive?
Always seeking an elegant solution.
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Reflection (About)
Oct 03, 2011 12:19 AM|LINK
Hello topolov,
I guess that you want to return a collection of a specific kind of Entity. So you can first define a function with two parameters:
1) T entity——entity model class.
2) object collection——for containing vlaues.
public void AutoGetValues<T>(T entity,object[]values) where T:TEntity
{
var results = entity.GetProperties();
values = new object[results.Length];
int i=0;
foreach (PropertyInfo p in results)
{
values[i++] = p.GetValue(entity,null);
}
}
3) And then call this method as:
object[] values = null;
AutoGetValues<ModelClass>(ModelClassInstance, values);
Topolov
Member
680 Points
411 Posts
Re: Reflection (About)
Oct 04, 2011 04:10 PM|LINK
Couldn´t make it work. Would you mind indicating me how to use with real life data like Northwind, Products, etc. I don´t know what to write in every step of the way. I have tried different combinations but still no luck
It seems that it requires something else that you already have taht I don´t as to work fine
I just copied and pasted the code in my OnInserting FormView event but it gives a lot of error messages
Please
----------------------
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Reflection (About)
Oct 05, 2011 02:36 AM|LINK
Hello Topolov:)
I've tipped you instead of doing all the things for you...
By the way. Here I've told you:
ModelClass is the model generated from real db tables, please use this instead.
Topolov
Member
680 Points
411 Posts
Re: Reflection (About)
Oct 05, 2011 06:03 PM|LINK
Sorry for asking but it is just because I don't understand
I'm not a sloppy person but I just don't get it
But it's ok. That had happened to me a lot here in this forum. From where I come we say that it's much bertter to ask twice than look like a fool the rest of your life so it seems natural for us asking when we reach a dead end road
If I were supposed to know as much as I should on the .ASP. Net Dynamic Data framework then I wouldn't come to ask here
Problem is that Dynamic Data literature is not really available from the manufacturer and to be true all the help environment of the official web site lacks of true examples and power
You must go somehwre else to find the answers
I have asked on this several times with no luck
How difficult can taht be to convert the word "Products" into something which .Net understands as the "Products" entity instead
There are many languages where this is done with no major problem at all but in .Net thins must go hand by hand with Intellisense and there you have the problem: not knowing what to do to make a simple substitution
That's sad
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Reflection (About)
Oct 06, 2011 01:04 AM|LINK
Hello Topolov:)
Would you mind showing us your modified codes as well as your model classes?
Thx again
Topolov
Member
680 Points
411 Posts
Re: Reflection (About)
Oct 06, 2011 02:27 PM|LINK
I must tell you that I was laid off 2 years ago and that I´m not in a good situation, financially speaking, now
That said, I must also explain you that I come to a cyber coffee to do my research. I have cut off my Internet services, cable, phone and yesterday I was also cut my electrical supply
Regarding what I´m doing to make a living is this: I´m trying to write a distributable invoicing application based on Dynamic Data. I started writing it almost a year ago. Haven´t finished yet because tehre are so many things regarding Dynamic data that are not really available for us anywhere else on the web but here and some blogs of some of your contributors
So I'll be glad to submit what you've asked but I have to prepare it and upload it to you which means anoyher day that I must wait
Having said all of that: I don´t understand what does my design have to do whit a simple question. All I need to know is this:
I have a string "Products". I also have other strings: names "as simple text" of the other NorthWind entities: "Suppliers", "Orders", etc.
I also have the string of the name of the DataModel which is declared in the global.asax file
All of them as strings
I need a way of "converting" those strings in something which might be usable when I pass them as parameteres for calling a method
I need a way of achieving "instantiating" those strings so I can have access to the properties of the entities they represent
Like:
var comingentity = table.Name;
(of which I got the string "Products")
[some kind of conversion here ??? ]
-- I don´t know what to do here to convert the string "Products" into the entity class Products
comingentity myentity = new comingentity();
which must act like if I were writing
Products myproducts = new Products();
and then I could use all of myproducts properties as If I had instantiated Products
This is silly but if you have 100 entities you wouldn´t like to create switch statements for instantiating a 100 different entities, would you like that?
So, as simple as that: How to convert a string which represents an entity name into the entity itself?
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Reflection (About)
Oct 07, 2011 01:11 AM|LINK
Hello Topolov:)
Don't worry and take it easy:)
Since you are using EntityFramework and Products should be a public property of that... You can have a try like this:
using (DbEntites dbe = new DbEntities)
{
dynamic products = dbe.GetType().GetProperty("Products").GetConstructor(Type.EmptyTypes).Invoke(null); //Here you can use GetConstructor method and Invoke to generate a new products list. And then do what you like...
}
Topolov
Member
680 Points
411 Posts
Re: Reflection (About)
Oct 11, 2011 12:27 AM|LINK
Ok, late answer from my side buty as I said I'm currently cybering as to reach the net
Not really lucky these days with money
I'm obligued to "unmark" the non-answer answer from Mr. Dong but his code doesn't work as expected
It doesn't work. GetConstructor is not available under taht type of construction
I've seen a previosu answer from him in which it works but then he uses Type at the begining of the line isntead of dbe whcih makes a huge difference in what are you doing and getting