Dynamic Database assignment

Source: Internet
Author: User
I want to dynamically assign values to ASP. NET 2.0 BLL based on the database fields, but it is not implemented yet. The problem is still not solved.
Through examples, we will talk about the application of reflection (vb version, details)
Http://www.graphics.net.cn/document/dotnet/001/200.asp

Dynamic Database assignment, based on XML and reflection
Data access layer http://www.zahui.com/html/14/34369.htm with ease

Overview of reflection http://blog.dev-club.esnai.com/cortex/archive/2005/11/25/3616.aspx
Http://www.codeproject.com/csharp/introreflection.asp

Below is C #
Http://www.pconline.com.cn/pcedu/empolder/net/0407/413536.html
(C #) Use reflection to dynamically call class members
To use reflection to dynamically call class members, you need a method of the type class: invokemember. The statement for this method is as follows (Excerpted from msdn ):

Public object invokemember (

String name,

Bindingflags invokeattr,

Binder binder,

Object target,

Object [] ARGs

);
Parameters

Name

String, which contains the name of the constructor, method, attribute, or field member to be called.

-Or-

An empty string ("") indicates the default Member of the call.

Invokeattr

One-bit blocking is composed of one or more bindingflags that specify the search execution method. Access can be one of bindingflags, such as public, nonpublic, Private, invokemethod, and getfield. You do not need to specify the search type. If the search type is omitted, bindingflags. Public | bindingflags. instance will be applied.

Binder

A binder object that defines a set of attributes and enables binding. Binding may involve selecting overload methods, mandatory parameter types, and calling members through reflection.

-Or-

If it is a null reference (nothing in Visual Basic), defaultbinder is used.

Target

The object on which the specified Member is to be called.

ARGs
An array containing parameters passed to the member to be called.

Return Value

Indicates the object returned by the called member.

Note:

The following bindingflags filter flag can be used to define members included in the search:

To obtain the returned value, bindingflags. instance or bindingflags. Static must be specified.

Specify bindingflags. Public to include public members in the search.

Specify bindingflags. nonpublic to include non-public members (Private Members and protected members) in the search ).

Specify bindingflags. flattenhierarchy to include static members in the hierarchy.

The following bindingflags modifier can be used to change the search execution method:

Bindingflags. ignorecase, indicating that the name case is ignored.

Bindingflags. declaredonly: searches for members declared on the type, rather than simply inherited members.

The following bindingflags call flag can be used to indicate the operations to be performed on members:

Createinstance, indicating to call the constructor. Ignore name. Invalid for other call flag.

Invokemethod indicates that a method is called, instead of a constructor or an initial value of the type. It is invalid for setfield or setproperty.

Getfield indicates obtaining the field value. It is invalid for setfield.

Setfield, indicating to set the field value. Invalid for getfield.

Getproperty indicates obtaining the property. It is invalid for setproperty.

Setproperty indicates setting properties. Invalid for getproperty.

The following example is a simple application of this method (I always thought that the simpler the example, the more intuitive the better, to make it easier to understand the meaning and function of the text .)

Using system;

Using system. reflection;

Namespace consoleapplication9

{

Class love

{

Public int field1;

Private string _ name;

Public love ()

{

}

Public string name

{

Get

{

Return _ name;

}

Set

{

_ Name = value;

}

}

Public int getint (int)

{

Return;

}

Public void display (string Str)
{

Console. writeline (STR );

}

}

/// <Summary>

/// Summary of class1.

/// </Summary>

Class class1

{

/// <Summary>

/// ApplicationProgram.

/// </Summary>

[Stathread]

Static void main (string [] ARGs)

{

//

// Todo: addCodeTo start the application

//

Love = new love ();

Type type = love. GetType ();

Object OBJ = type. invokemember (null,

Bindingflags. declaredonly |

Bindingflags. Public | bindingflags. nonpublic |

Bindingflags. instance | bindingflags. createinstance, null, null, argS );
// Call a method without a return value

Type. invokemember ("display", bindingflags. invokemethod | bindingflags. Public | bindingflags. instance, null, OBJ, new object [] {"aldfjdlf "});

// Call a method with a returned value

Int I = (INT) type. invokemember ("getint", bindingflags. invokemethod | bindingflags. Public | bindingflags. instance, null, OBJ, new object [] {1 });

Console. writeline (I );

// Set the attribute value

Type. invokemember ("name", bindingflags. setproperty, null, OBJ, new string [] {"ABC "});

// Obtain the property value

String STR = (string) type. invokemember ("name", bindingflags. getproperty, null, OBJ, null );

Console. writeline (STR );

// Set the field value

Type. invokemember ("field1", bindingflags. setfield, null, OBJ, new object [] {444 });

// Obtain the field value

Int F = (INT) type. invokemember ("field1", bindingflags. getfield, null, OBJ, null );

Console. writeline (f );

Console. Readline ();

}

}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.