Dynamically assign values to instance
-
Basically, you are searching for an alternative to Reflection, right? Calin
-
Kind of. Something that does the job but keeps the speed on same level as direct access.
zilo
I think you could try by using Dynamic Invocation. Calin
-
I think you could try by using Dynamic Invocation. Calin
-
Do you suggest to create a method using reflection on the fly:
void AssingAValue(AClass aInstance, object value)
{
aInstance.AValue = value;
}and then just call this method?
zilo
yes, so you can dynamically change the
AValue
property. Calin -
Yes. I'm loading the class from dll as
Assembly.LoadFile(string file);
And I'd like to keep AClass as simple as possible, without any specific code to read or write values into it. It's just a data container.
zilo
Well, LINQ knows the types of data objects at compile time, so there's no similarity there. Without reflection, I guess your best bet is emitting the IL directly[^].
Regards Senthil [MVP - Visual C#] _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro
-
Interesting idea, but... It's not only one, there will be many properties like this within AClass. And it needs to be done outside of constructor.
zilo
Maybe extension methods could work. Create an extension method ToAClass for each type, and also pass the AClass instance.
public static class ExtensionMethods
{
static public void SetAClass(this string value, AClass instance)
{
instance.AString = value;
}static public void SetAClass(this int value, AClass instance) { instance.AInt = value; }
}
public class AClass
{
public string AString
{
get;
set;
}
public int AInt
{
get;
set;
}
}AClass aInstance = new AClass();
"Test string".SetAClass(aInstance);
123.SetAClass(aInstance);Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) -
Maybe extension methods could work. Create an extension method ToAClass for each type, and also pass the AClass instance.
public static class ExtensionMethods
{
static public void SetAClass(this string value, AClass instance)
{
instance.AString = value;
}static public void SetAClass(this int value, AClass instance) { instance.AInt = value; }
}
public class AClass
{
public string AString
{
get;
set;
}
public int AInt
{
get;
set;
}
}AClass aInstance = new AClass();
"Test string".SetAClass(aInstance);
123.SetAClass(aInstance);Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) -
Well, LINQ knows the types of data objects at compile time, so there's no similarity there. Without reflection, I guess your best bet is emitting the IL directly[^].
Regards Senthil [MVP - Visual C#] _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro
-
It doesn't have to, I'm using those classes to work with linq as well and it's working fine. Thanks for the suggestion, that looks to be the only way to go.
zilo
Zilo(svk) wrote:
I'm using those classes to work with linq as well and it's working fine
Now I'm curious - can you paste a snippet of code that does that? I can't imagine LINQ working without you specifying the type somewhere (unless it's an anonymous type, of course).
Regards Senthil [MVP - Visual C#] _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro
-
Zilo(svk) wrote:
I'm using those classes to work with linq as well and it's working fine
Now I'm curious - can you paste a snippet of code that does that? I can't imagine LINQ working without you specifying the type somewhere (unless it's an anonymous type, of course).
Regards Senthil [MVP - Visual C#] _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro
I ment the classes are not present withing the same solution or assembly when compiling the code. When working with linq, I'm using strong types. This is a sample of one of the classes:
[Table(Name = "Books")]
public partial class Book : IDALEntity
{
private int _BookId;
private string _Title;
private int _Price;
private int _PublisherID;public Book() { } \[Column(Storage = "\_BookId", AutoSync = AutoSync.OnInsert, DbType = "Int NOT NULL", IsPrimaryKey = true)\] public int BookId { get { return this.\_BookId; } set { this.\_BookId = value; } }
...
}zilo
-
I ment the classes are not present withing the same solution or assembly when compiling the code. When working with linq, I'm using strong types. This is a sample of one of the classes:
[Table(Name = "Books")]
public partial class Book : IDALEntity
{
private int _BookId;
private string _Title;
private int _Price;
private int _PublisherID;public Book() { } \[Column(Storage = "\_BookId", AutoSync = AutoSync.OnInsert, DbType = "Int NOT NULL", IsPrimaryKey = true)\] public int BookId { get { return this.\_BookId; } set { this.\_BookId = value; } }
...
}zilo
Well, LINQ uses reflection to read the custom attributes you provide for each property, so that it can map them to database columns. I guess they do it just once and then generate dynamic code to do the actual translation from SQL results to object property assignments.
Regards Senthil [MVP - Visual C#] _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro
-
Kind of. Something that does the job but keeps the speed on same level as direct access.
zilo
Also, you can use
TypeDescriptor
, andPropertyDescriptor
. Calin