private static void write(Chunk chunk, StreamAccessor accessor) { accessor.Write(chunk.name, EncodingType.CC4); StreamAccessor ac2 = new StreamAccessor(accessor.WriteSubStream(4)); long pos0 = accessor.Position; // 内容 if (chunk.content != null) { if (chunk.content is byte[]) { accessor.Write((byte[])chunk.content); } else if (chunk.content is System.IO.Stream) { accessor.WriteStream((System.IO.Stream)chunk.content); } else { RiffChunkReadWriteAttribute.Write(chunk.content, accessor); } } else if (chunk._data != null) { accessor.Write(chunk._data); } else if (chunk._stream != null) { accessor.WriteStream(chunk.Stream); } ac2.Write(checked ((uint)(accessor.Position - pos0)), EncodingType.U4); ac2.Stream.Close(); }
internal static void Write(object value, StreamAccessor accessor) { System.Type type = value.GetType(); RiffChunkReadWriteAttribute attr = GetAttribute(type); const System.Reflection.BindingFlags BF = System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic; System.Reflection.MethodInfo m = type.GetMethod(attr.write, BF, null, new System.Type[] { type, typeof(StreamAccessor) }, read_mods); m.Invoke(null, new object[] { value, accessor }); }
/// <summary> /// Chunk の内容を指定した型のインスタンスとして取得します。 /// </summary> /// <typeparam name="T">取得するインスタンスの型を指定します。以下の物を指定できます。 /// 1. <see cref="byte"/>[] /// 2. <see cref="System.IO.Stream"/> /// 3. RiffChunkReadWriteAttribute 属性を適用した型 /// </typeparam> /// <returns>指定した型のインスタンスを返します。</returns> public T GetContent <T>() { if (typeof(T) == typeof(byte[])) { return((T)(object)this.Data); } if (typeof(T) == typeof(System.IO.Stream)) { return((T)(object)this.Stream); } return((T)RiffChunkReadWriteAttribute.Read(typeof(T), new StreamAccessor(this.Stream))); }
private static RiffChunkReadWriteAttribute GetAttribute(System.Type type) { if (!attr_cache.ContainsKey(type)) { RiffChunkReadWriteAttribute[] attrs = (RiffChunkReadWriteAttribute[])type.GetCustomAttributes(typeof(RiffChunkReadWriteAttribute), false); attr_cache[type] = attrs.Length == 0?null:attrs[0]; } RiffChunkReadWriteAttribute attr = attr_cache[type]; if (attr == null) { throw new System.ApplicationException("指定した型を RIFF Chunk として読み書きする事は出来ません。"); } return(attr); }
internal static object Read(System.Type type, StreamAccessor accessor) { RiffChunkReadWriteAttribute attr = GetAttribute(type); const System.Reflection.BindingFlags BF = System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic; System.Reflection.MethodInfo m = type.GetMethod(attr.read, BF, null, read_types, read_mods); object ret = m.Invoke(null, new object[] { accessor }); if (ret == null) { throw new System.ApplicationException("RIFF Chunk 読込の為に呼び出したメソッドの返値が null です。"); } return(ret); }