RE: OptionalFieldAttribute not needed with BinaryFormatter in .net
- From: jetan@xxxxxxxxxxxxxxxxxxxx ("Jeffrey Tan[MSFT]")
- Date: Mon, 10 Jul 2006 08:20:46 GMT
Hi Andy,
Thanks for your feedback!
I guess they're talking about v1 in that sentence?Yes, I assume he is talking about .Net1.1. Also, since this article is
written in October 2004, which .Net2.0 is still in beta version, if the
author is talking about .Net2.0, I assume that this behavior has changed in
RTM.
1) Is there a way to get the rules to apply consistently?Sorry, I am not sure I understand you completely. Can you provided more
information regarding the "dictionary classes", which classes in FCL do you
refer to? Is it possible for you to provide a little sample project for us
to reproduce this problem? Thanks
2) Is there another way to do this,I have performed some research in this issue and found the key point lies
perhaps some attribute I can attach to my class, rather than having to
interact with the formatter?
in the System.Runtime.Serialization.Formatters.Binary.ObjectReader.Bind
method and
System.Runtime.Serialization.Formatters.Binary.ObjectReader.FastBindToType
method.(From Reflector), below is the code snippet:
internal Type Bind(string assemblyString, string typeString)
{
Type type1 = null;
if ((this.m_binder != null) && !this.IsInternalType(assemblyString,
typeString))
{
type1 = this.m_binder.BindToType(assemblyString, typeString);
}
if (type1 == null)
{
type1 = this.FastBindToType(assemblyString, typeString);
}
return type1;
}
internal Type FastBindToType(string assemblyName, string typeName)
{
.....
if (this.bSimpleAssembly)
{
try
{
ObjectReader.sfileIOPermission.Assert();
try
{
StackCrawlMark mark1 =
StackCrawlMark.LookForMe;
assembly2 =
Assembly.LoadWithPartialNameInternal(assemblyName, null, ref mark1);
if ((assembly2 == null) && (assemblyName !=
null))
{
assembly2 =
Assembly.LoadWithPartialNameInternal(new AssemblyName(assemblyName).Name,
null, ref mark1);
}
}
finally
{
CodeAccessPermission.RevertAssert();
}
}
catch (Exception)
{
}
}
else
{
try
{
ObjectReader.sfileIOPermission.Assert();
try
{
assembly2 = Assembly.Load(assemblyName);
}
finally
{
CodeAccessPermission.RevertAssert();
}
}
catch (Exception)
{
}
}
.....
}
As we can see, FastBindToType checks bSimpleAssembly field(which maps to
FormatterAssemblyStyle.Full/Simple) to use Assembly.Load or
Assembly.LoadWithPartialNameInternal. From Applied .net Framwork written by
"Jeffrey Richter", we know that Assembly.Load will check the version
explicitly, while Assembly.LoadWithPartialNameInternal will ignore the
assembly version.
Also, in Bind method, we can see that Binder.BindToType method gives us a
hook over the FastBindToType method, which may help us to jmp over
FastBindToType method.
OptionalFieldAttribute is used in
System.Runtime.Serialization.Formatters.Binary.ReadObjectInfo.GetMemberTypes
method:
internal Type[] GetMemberTypes(string[] inMemberNames, Type objectType)
{
......
if (!flag2)
{
object[] objArray1 =
this.cache.memberInfos[num2].GetCustomAttributes(typeof(OptionalFieldAttribu
te), false);
if (((objArray1 == null) || (objArray1.Length ==
0)) && !this.bSimpleAssembly)
{
throw new
SerializationException(string.Format(CultureInfo.CurrentCulture,
Environment.GetResourceString("Serialization_MissingMember"), new object[]
{ this.cache.memberNames[num2], objectType,
typeof(OptionalFieldAttribute).FullName }));
}
}
}
}
return typeArray1;
}
So FCL uses System.Reflection.MemberInfo.GetCustomAttributes method to
access the OptionalFieldAttribute. If you want to determine if there is any
additionally attribute can meet your need without changing the formatter
itself, you may set a breakpoint in
System.Reflection.MemberInfo.GetCustomAttributes method in debugger and
monitor if Deserialize method will trigger other breakpoint that is not
looking for OptionalFieldAttribute. Based on my research, I can not find a
such attribute. So you may still have to touch the formatter itself to hook
the process.
Hope this helps. If you have any further concern or need any other help,
please feel free to tell me, thanks!
Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
.
- Follow-Ups:
- References:
- RE: OptionalFieldAttribute not needed with BinaryFormatter in .net v2.
- From: "Jeffrey Tan[MSFT]"
- RE: OptionalFieldAttribute not needed with BinaryFormatter in .net v2.
- Prev by Date: RE: OptionalFieldAttribute not needed with BinaryFormatter in .net v2.
- Next by Date: Cross-AppDomain calls and different assmbly versions
- Previous by thread: RE: OptionalFieldAttribute not needed with BinaryFormatter in .net v2.
- Next by thread: RE: OptionalFieldAttribute not needed with BinaryFormatter in .net
- Index(es):
Loading