Некоректные исполнение методов HasNamespace, LookupNamespace
От: Лёлик Россия www.dosug.nu
Дата: 25.04.05 13:40
Оценка:
Народ- а почему так получается?


using System;
using System.Xml;
using System.Text;

class Test
{
    public static void Main()
    {
        XmlNamespaceManager nsm = new XmlNamespaceManager(new NameTable());
        string ns1 = "ab";
        nsm.AddNamespace(ns1, "urn:my-schema");
        StringBuilder sb = new StringBuilder();
        sb.Append("a");
        sb.Append("b");
        string ns2 = sb.ToString();
        Console.WriteLine( "Object.ReferenceEquals(ns1, ns2) : " + Object.ReferenceEquals(ns1, ns2) );
        Console.WriteLine( ns1 + " : " + nsm.LookupNamespace(ns1) );
        Console.WriteLine( ns2 + " : " + nsm.LookupNamespace(ns2) );
        
        ns2 = string.Intern(ns1);
        Console.WriteLine( "Object.ReferenceEquals(ns1, ns2) : " + Object.ReferenceEquals(ns1, ns2) );
        Console.WriteLine( ns1 + " : " + nsm.LookupNamespace(ns1) );
        Console.WriteLine( ns2 + " : " + nsm.LookupNamespace(ns2) );
    }
}

Возвращает


Object.ReferenceEquals(ns1, ns2) : False
ab : urn:my-schema
ab :
Object.ReferenceEquals(ns1, ns2) : True
ab : urn:my-schema
ab : urn:my-schema

BUG?
Re: Некоректные исполнение методов HasNamespace, LookupNames
От: Chupa_Kabra  
Дата: 26.04.05 03:09
Оценка:
Здравствуйте, Лёлик, Вы писали:

Лё>Народ- а почему так получается?

Лё> BUG?

MSDN:string.intern

The common language runtime conserves string storage by maintaining a table, called the intern pool, that contains a single reference to each unique literal string declared or created programmatically in your program. Consequently, an instance of a literal string with a particular value only exists once in the system.

For example, if you assign the same literal string to several variables, the runtime retrieves the same reference to the literal string from the intern pool and assigns it to each variable.

The Intern method uses the intern pool to search for a string equal to the value of str. If such a string exists, its reference in the intern pool is returned. If the string does not exist, a reference to str is added to the intern pool, then that reference is returned.

In the C# example that follows, the string, s1, which has a value of "MyTest", is already interned because it is a literal in the program.

The System.Text.StringBuilder class generates a new string object that has the same value as s1. A reference to that string is assigned to s2.

The Intern method searches for a string that has the same value as s2. Since such a string exists, the method returns the same reference that is assigned to s1, then that reference is assigned to s3.

References s1 and s2 compare unequal because they refer to different objects, while references s1 and s3 compare equal because they refer to the same string.

String s1 = "MyTest";
String s2 = new StringBuilder().Append("My").Append("Test").ToString();
String s3 = String.Intern(s2);
Console.WriteLine((Object)s2==(Object)s1); // Different references.
Console.WriteLine((Object)s3==(Object)s1); // The same reference.

Compare this method to the IsInterned method.

Все хотят хорошо провести время, но время не проведешь !
Re[2]: Некоректные исполнение методов HasNamespace, LookupNa
От: Лёлик Россия www.dosug.nu
Дата: 26.04.05 06:28
Оценка:
весь трагизм ситуации в том, что в методах HasNamespace, LookupNamespace и пр. для сравнения префиксов используется internal класс Ref, который сравнивает ссылки на объекты, а не строки.
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.