ResourceEntry
Represents resource entry.
using System;
using System.Globalization;
using System.IO;
using System.Resources;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace DotNext.Resources
{
[StructLayout(LayoutKind.Auto)]
[NullableContext(1)]
[Nullable(0)]
public readonly struct ResourceEntry
{
public string Name { get; }
public ResourceManager Manager { get; }
internal ResourceEntry(ResourceManager manager, string name)
{
if (manager == null)
throw new ArgumentNullException("manager");
Manager = manager;
if (name == null)
throw new ArgumentNullException("name");
Name = name;
}
public string AsString([Nullable(2)] CultureInfo culture = null)
{
string string = Manager.GetString(Name, culture ?? CultureInfo.CurrentUICulture);
if (string == null)
throw new InvalidOperationException(ExceptionMessages.ResourceEntryIsNull(Name));
return string;
}
public string Format([Nullable(new byte[] {
1,
2
})] params object[] args)
{
CultureInfo currentUICulture = CultureInfo.CurrentUICulture;
return string.Format(currentUICulture, AsString(currentUICulture), args);
}
public Stream AsStream([Nullable(2)] CultureInfo culture = null)
{
UnmanagedMemoryStream stream = Manager.GetStream(Name, culture ?? CultureInfo.CurrentUICulture);
if (stream == null)
throw new InvalidOperationException(ExceptionMessages.ResourceEntryIsNull(Name));
return stream;
}
[NullableContext(2)]
[return: Nullable(1)]
public T As<T>(CultureInfo culture = null)
{
object object = Manager.GetObject(Name, culture);
if (!(object is T))
throw new InvalidOperationException();
return (T)object;
}
public static explicit operator string([In] [IsReadOnly] ref ResourceEntry entry)
{
return entry.AsString(null);
}
public static explicit operator Stream([In] [IsReadOnly] ref ResourceEntry entry)
{
return entry.AsStream(null);
}
}
}