Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Source Generator生成报错 #137

Closed
526077247 opened this issue Jan 12, 2025 · 2 comments
Closed

Source Generator生成报错 #137

526077247 opened this issue Jan 12, 2025 · 2 comments

Comments

@526077247
Copy link

Source Generator生成报错

报错类型
public class MultiMap<T, K>: SortedDictionary<T, List<K>>{}

报错定义
MultiMap<long, long> TimeId = new MultiMap<long, long>();

生成的代码

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static void Deserialize(out TaoTie.MultiMap<long, long> value, ref Reader reader)
        {
        #if WEAK_VERSION_TOLERANCE
             if (reader.Eof)
             {
                value = default;
                return;
             }
        #endif
            
            if (!reader.ReadCollectionHeader(out var length))
            {
                value = default;
                return;
            }
            
            value = new TaoTie.MultiMap<long, long>();
            for (int i = 0; i < length; i++)
            {
                Deserialize(out KeyValuePair<long, long> kvp, ref reader); //错误的地方
                value[kvp.Key] = kvp.Value;  //错误的地方
            }
        }
@JasonXuDeveloper
Copy link
Owner

可以试试给MultiMap实现个indexer,这样就不会报错了

@JasonXuDeveloper
Copy link
Owner

JasonXuDeveloper commented Jan 13, 2025

已修复,马上推送新release,不过这个做法需要实现索引器:

public class MultiMap<T, K> : SortedDictionary<T, List<K>>
{
    
    public new List<K> this[T key]
    {
        get
        {
            if (!TryGetValue(key, out List<K> value))
            {
                value = new List<K>();
                Add(key, value);
            }

            return value;
        }
        
        set
        {
            if (value.Count == 0)
            {
                Remove(key);
            }
            else
            {
                base[key] = value;
            }
        }
    }
}

等下会在文档内提一嘴

JasonXuDeveloper added a commit that referenced this issue Jan 13, 2025
- [fix] fixed source generator cannot detect the correct dictionary type parameters for a user defined dictionary subclass (#137)
JasonXuDeveloper added a commit that referenced this issue Jan 13, 2025
- [fix] fixed source generator cannot detect the correct dictionary type parameters for a user defined dictionary subclass (#137)
JasonXuDeveloper added a commit that referenced this issue Jan 13, 2025
- [fix] fixed source generator cannot detect the correct dictionary type parameters for a user defined dictionary subclass (#137)
JasonXuDeveloper added a commit that referenced this issue Jan 13, 2025
- [fix] fixed source generator cannot detect the correct dictionary type parameters for a user defined dictionary subclass (#137)
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants