Ejemplo n.º 1
0
        public void Register <T>(CtxReadDelegate <T> reader, CtxWriteDelegate <T> writer, int?predefinedId = null)
        {
      #if !NET35
            if (!myBackgroundRegistrar.IsInsideProcessing)
            {
                myBackgroundRegistrar.SendBlocking(new ToplevelRegistration(typeof(T), szr => szr.Register(reader, writer, predefinedId)));
                myBackgroundRegistrar.WaitForEmpty();
            }
      #endif

            var  typeId = RdId.Define <T>(predefinedId);
            RdId existing;
            if (myTypeMapping.TryGetValue(typeof(T), out existing))
            {
                Assertion.Require(existing == typeId, "Type {0} already present with id={1}, but now is set with {2}", typeof(T).FullName, existing, typeId);
            }
            else
            {
                if (myReaders.ContainsKey(typeId))
                {
                    var existingType = myTypeMapping.First(p => p.Value == typeId).Key;
                    throw new ArgumentException(string.Format("Can't register {0} with id {1}. Already registered {2}", typeof(T).FullName, typeId, existingType));
                }
                Protocol.InitializationLogger.Trace("Registering type {0}, id={1}", typeof(T).Name, typeId);

                myTypeMapping[typeof(T)] = typeId;
                myReaders[typeId]        = (ctx, unsafeReader) => reader(ctx, unsafeReader);
                myWriters[typeId]        = (ctx, unsafeWriter, value) => writer(ctx, unsafeWriter, (T)value);
            }
        }
Ejemplo n.º 2
0
        public void Register <T>(CtxReadDelegate <T> reader, CtxWriteDelegate <T> writer, long?predefinedId = null)
        {
            lock (myLock)
            {
                var  typeId = RdId.Define <T>(predefinedId);
                RdId existing;
                if (myTypeMapping.TryGetValue(typeof(T), out existing))
                {
                    Assertion.Require(existing == typeId, "Type {0} already present with id={1}, but now is set with {2}", typeof(T).FullName, existing, typeId);
                }
                else
                {
                    if (myReaders.ContainsKey(typeId))
                    {
                        Type existingType;
                        lock (myLock)
                            existingType = myTypeMapping.First(p => p.Value == typeId).Key;
                        throw new ArgumentException(string.Format("Can't register {0} with id {1}. Already registered {2}", typeof(T).FullName, typeId, existingType));
                    }
                    Protocol.InitTrace?.Log($"Registering type {typeof(T).Name}, id={typeId}");

                    myTypeMapping[typeof(T)] = typeId;
                    myReaders[typeId]        = (ctx, unsafeReader) => reader(ctx, unsafeReader);
                    myWriters[typeId]        = (ctx, unsafeWriter, value) => writer(ctx, unsafeWriter, (T)value);
                }
            }
        }
Ejemplo n.º 3
0
 public void AddType(Type type)
 {
     myRdIdToTypeMapping[RdId.Define(type)] = type;
 }