Tuesday, June 22, 2004

Metaclass in C#

I'm writing a class library and have to implement a feature close to XML serialization. The problem is I am not that guy who declares the scheme for serialized data. And this scheme may be expanded and extended in the future and certainly I wish to reuse the code. The scheme is designed such that there is no common algorithm to determine classname for object represented with an element. The classes are arranged in groups. Every group has its own algorithm to determine class or subgroup to which the obect belongs. If the algorithm detects only a subgroup then it calls to corresponding algorithm of that group.

For example, we have four classes B, C, E, F arranged in two groups A and D. Group D contains classes E and F, while A contains classes B and C and gourp D. I have defined the groups as highlevel classes in the hierarhy, while leaves are actual classes.

  • A
    • B
    • C
    • D
      • E
      • F

In XML objects look like

<B>B instance </B>
<C>C instance</C>
<D Type=”E”></D>
<D Type=”F”></D>

Every highlevel class has static method for the detection algorithm. I use reflection to obtain the method and transfer control to it. But I’d like to solve this task in the following way.

class AClass : Type
{
object CreateInstance( XmlElement e )
{
AClass C = Detect( e );
return C.InvokeMember( CreateInstance, new object[]{e} );
}
virtual AClass Detect( XmlElement e )
{
return this;
}
}
class DClass : AClass
{
override AClass Detect( XmlElement e )
{
AClass C = (AClass) Type.GetType( “MyNamespace.GroupA.GroupD.” + e.GetAttribute(“Type”) );
return C.Detect(e);
}
}
[Metaclass(typeof(AClass))]
abstract class A
{
A( XmlElement e ){...}
}
class B : A {...}
class C : A {...}
[Metaclass(typeof(DClass))]
class D : A{...}
namespace GroupD{
[Metaclass(typeof(AClass))]
class E : D {...}
[Metaclass(typeof(AClass))]
class F: D {...}
}

The loading object from XMl would look like

XmlElement e = (XmlElement) someXmlDoc.SelectSingleNode( someXPath );
AClass C = (AClass) Type.GetType( “MyNamespace.GroupA.” + e.Name );
A a =(A) C.CreateInstance( e );

The only new thig is MetaclassAttribute wich provides metaclass for the class being declared.

Certaily I could create some helper objects, containig detection methods. But this approach woul require creating of instances of those detection classes. Why should I instantiate new objects while .NET framework already created objects for the classes? I think the class objects should carry all the information on classes they define. The information could include some progrmmer-defined items. C# has already static memrebs. They in fact are pieces of such programmer's data on the class. MetaclassAttribute will generalize this feature.

We have run-time type information provided by .NET framework. Then we should use this information, because it's too costly to handle it for nothing.

Imagine a plug-in anabled application. It includes a plug-in management code. But the only thing a plug-in does was providing a managed class to the application. We use classes to simplify object management. But when classes became objects themselves we could use the same tool to simplify managing classes.

Another feature is the possibility to restrict method parameters of type Type to a specific set of classes.

6 Comments:

At 3:16 PM, Blogger Aleksei Guzev said...

There is one more way to accomplish this task.

I could define a separate class for loading objects from XML and assign its instances to the classes as attributes.

While this is not such a performance issue as using reflection to find the static method, this approach still involves creating of an additioanl object.

 
At 1:28 AM, Anonymous Anonymous said...

Have you looked at TypeDelegator.

A TypeDelegator is proxy for a Type. You can override the methods you want, and in all other respects an instance of TypeDelegator behaves like the type is delegates to.

 
At 12:18 PM, Blogger dilh20izxz said...

Big News From The Healthcare Industry!!

+++++++++++Current Profile+++++++++++
Faceprint Global Solutions (FCPG)
Current Price $0.15
A company with hot new identity solution products
and licenses with over 40 current governmental and
non-governmental contracts in negotiations.
+++++++++++++++++++++++++++++++++

Is this an undiscovered gem priced to go higher!!
Please read the following Announcement in its Entirety and Consider the Possibilities´┐Ż
Watch this One to Trade!

FCPG volume trading is beginning to surge with this landslide Announcement. The value of this
stock appears poised for growth! This one should not remain on the ground floor for long.

BREAKING NEWS!!

Faceprint Global Solutions (FCPG) is pleased to announce that its European partner, Keyvelop, has teamed up with IBM's Partner World Industry Networks to deliver customer software requirement solutions for the international healthcare industry.
With FGS owning the exclusive North American rights to distribute the worlds leading encryption and transmission software developed by Keyvelop, FGS is poised to capture large volumes of sales generated by customers currently using IBM's software in the healthcare and other industries.
With traceability and security now deemed a serious business priority, companies are increasingly focused on employing procedures and controls designed to ensure the authenticity, integrity and confidentiality of electronic records.
"This is a very positive move for FGS and for Keyvelop," said FGS CEO Pierre Cote. "We are very happy about the decision to go with IBM. This is a continuation of the progress made by everyone associated with FGS and its partners."
Buell Duncan, IBM's general manager of ISV & Developer Relations commented, "Collaborating with Keyvelop will ensure that we develop open solutions that are easy to maintain and cost effective for our customers in the healthcare and life sciences industry."
Among other things, this new software technology which is currently being used by a number of European healthcare companies, is used to send any file, regardless of format or size. Encryption keys, evidence of transmission integrity with fingerprint calculation, time-stamping of all actions and status record updating, pre-checking sender and receiver identities, validating file opening dates are part of Keyvelop features.
About FacePrint Global Solutions, Inc.
FGS operates a business, which develops and delivers a variety of technology solutions, including biometric software applications on smart cards and other support mediums (apometric solutions). FGS's products provide biometric solutions for identity authentication and a host of smart card- and biometrics-related hardware peripherals and software applications. Apometrix, FGS's wholly-owned subsidiary, combines on-card or in-chip multi-application management solutions with best-of-breed 'in-card matching' biometrics. Keyvelop's secure digital envelope solution and Apometrix's on-card biometrics work together to produce the winning combination in the fields of security, traceability and identity management. FGS is headquartered in Fresno, California.
Conclusion:

The examples above show the Awesome, Earning Potential of little known Companies That Explode onto Investor's Radar Screens. This stock will not be a Secret for long. Then You May Feel the Desire to Act Right Now! And Please Watch This One Trade!
GO FCPG!

All statements made are our express opinion only and should be treated as such. We may own, take position and sell any securities mentioned at any time. Any statements that express or involve discussions with respect to predictions, goals, expectations, beliefs, plans, projections, objectives, assumptions or future events or performance are not statements of historical fact and may be "forward looking statements." Forward looking statements are based on expectations, estimates and projections at the time the statements are made that involve a number of risks and uncertainties which could cause actual results or events to differ materially from those presently anticipated. This profile is in no way affiliated with the featured company. We were compensated one thousand dollars from third party (IR Marketing) to distribute this report. Forward looking statements in this action may be identified through the use of words such as: "projects", "foresee", "expects". in compliance with Section 17(b), we disclose the holding of FGS shares prior to the publication of this report. Be aware of an inherent conflict of interest resulting from such holdings due to our intent to profit from the liquidation of these shares. Shares may be sold at any time, even after positive statements have been made regarding the above company. Since we own shares, there is an inherent conflict of interest in our statements and opinions. Readers of this publication are cautioned not to place undue reliance on forward-looking statements, which are based on certain assumptions and expectations involving various risks and uncertainties that could cause results to differ materially from those set forth in the forward- looking statements. This is not solicitation to buy or sell stocks, this text is for informational purpose only and you should seek professional advice from registered financial advisor before you do anything related with buying or selling stocks, penny stocks are very high risk and you can lose your entire investment.

 
At 12:09 AM, Blogger lisamartin5467044687 said...

I read over your blog, and i found it inquisitive, you may find My Blog interesting. My blog is just about my day to day life, as a park ranger. So please Click Here To Read My Blog

http://www.juicyfruiter.blogspot.com

 
At 10:01 AM, Blogger pl7wei66 said...

hey, I just got a free $500.00 Gift Card. you can redeem yours at Abercrombie & Fitch All you have to do to get yours is Click Here to get a $500 free gift card for your backtoschool wardrobe

 
At 6:34 PM, Blogger bethwarner9467202565 said...

hey, I just got a free $500.00 Gift Card. you can redeem yours at Abercrombie & Fitch All you have to do to get yours is Click Here to get a $500 free gift card for your backtoschool wardrobe

 

Post a Comment

<< Home