RE: LinqDataSource Adding Row

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Hello Check,
Thanks for your reply.

It seems returned object is dynamically created in LinqDataSource's
Selecting event with select statement. Anonymous type object hasn't default
constructor. This results the error when running "object ojb =
Activator.CreateInstance(typeObj)" statement. In such scenario, my
suggestion is to create each argument instance and pass them into Anonymous
type constructor as below. The following code is what I tested on my side.
It should also works with default contructor. Let me know if you face any
issue on it.

void LinqDataSource1_Selected(object sender, LinqDataSourceStatusEventArgs
e)
{
Type typeList = e.Result.GetType(); //List<T> for a select
statement
Type typeObj = e.Result.GetType().GetGenericArguments()[0];
//<T>

// Arguments list for <T> constructor.
{
ArrayList typeObjArguments = new ArrayList();//
foreach (Type t in typeObj.GetGenericArguments())
{
typeObjArguments.Add(Activator.CreateInstance(t));
}
}

object ojb = Activator.CreateInstance(typeObj,
typeObjArguments.ToArray()); //new T
// insert the new T into the list by using InvokeMember on the
List<T>
object result = null;
object[] arguments = { 0, ojb };

result = typeList.InvokeMember("Insert",
BindingFlags.InvokeMethod, null, e.Result, arguments);
}

Hope this helps. Please feel free to let us know if you have any more
concern. We are glad to assist you.
Best regards,
Wen Yuan

Microsoft Online Community Support
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg@xxxxxxxxxxxxxx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

.



Relevant Pages

  • Re: OOP
    ... So constructor creates object and initializes it ... function Employee(dept, manager) { ... var employee1 = new Employee ... Prototype keeps *shared* properties/methods ...
    (comp.lang.javascript)
  • Re: STL map of structures
    ... to add a default constructor and a copy constructor to my structure.". ... Microsoft Online Community Support ... Please feel free to let my manager ... or a Microsoft Support Engineer within 1 business day is acceptable. ...
    (microsoft.public.dotnet.languages.vc)
  • Re: Referencing an objects parent
    ... > Create a base ancestor class for your manager, ... And pass a pointer to the manager instance (i.e. pass ... constructor Create; ... so that I can target an object on any compatable form that creates the ...
    (borland.public.delphi.language.objectpascal)
  • Re: Using static factories to create two objects with bidirectional linking
    ... Right now I'm making some kind of manager class, which, for a static ... Alternatively, you could have some other factory class/method which creates both and connects them, but then both classes must have setters for instances of X/Y. ... If you want to make one to one association imutable, you might consider the constructor solution. ...
    (comp.object)
  • Re: Referencing an objects parent
    ... Create a base ancestor class for your manager, ... Manager1: TMyThreadManager; ... constructor TMyThread.Create; ... > different manager object. ...
    (borland.public.delphi.language.objectpascal)