์๋
ํ์ธ์.
ํญ์ ๋ํ
๋ง ํ๋ค๊ฐ ๊ถ๊ธํ๊ฒ ์๊ฒจ ์ด๋ ๊ฒ ๊ธ ๋จ๊น๋๋ค. ใ
ใ
(ํญ์ ์ข์๋ด์ฉ๋ค ์๊ฐํด์ฃผ์๊ณ ์๋ ค์ฃผ์
์ ๊ฐ์ฌํฉ๋๋ค.)
์ง๋ฌธ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค. (์ ๋ชฉ์ด ์ฐธ ์ ๋งคํ๋ค์,)
generic์ผ๋ก ๋ฐฐ์ด์ ๋ฐ๊ณ , ํด๋น ๋ฐฐ์ด์ params ๋ฅผ ์ฌ์ฉํ๋ ํจ์๋ก ๋๊ธฐ๋ ค ํ์๋๋ฐ, ์ด์ํ๊ฒ ์๋ ์ฌ์ง์ฒ๋ผ ๋ค์ด์ต๋๋ค.
์๋๋ ๊ฐ๋จํ ๊ตฌ์ฑํ ์์ ์
๋๋ค.
์์ธํ ๋ด์ฉ์ ์ฃผ์์ ์์ฑํด ๋์์ต๋๋ค.
public class Main
{
public class Main()
{
var testClass = new TestClass<SomeClass, object[]>();
// ์ด์ํ๊ฒ ์๋
testClass.MainFunc(new object[] { "str1", "str2" });
// ์ ์์๋
testClass.MainFunc2(new object[] { "str1", "str2" });
}
}
public class TestClass<TEntity, TKey>
where TEntity : class
{
public void MainFunc(TKey key)
{
TestFunc(key);
}
public void MainFunc2(object[] objectArray)
{
TestFunc(key);
}
public void TestFunc(params object[] objectArray)
{
// MainFunc์ ๊ฒฝ์ฐ ์ฌ๊ธฐ์ objectArray์ Length๊ฐ 2๊ฐ ์๋ 1์ด๊ณ , 2์ฐจ์ ๋ฐฐ์ด์ด ๋ค์ด์ต๋๋ค.
// ์ด๋ป๊ฒ ํด์ผ Length๊ฐ 2์ธ ๋ฐฐ์ด๊ฐ์ ์ป์ ์ ์์๊น์?
// ๋ํ ์ด๋ค ์๋ฆฌ์ ์ํด ์ด๋ฐ์ผ์ด ๋ฐ์ํ๋ ๊ฒ์ผ๊น์?
}
}
๊ฐ์ฌํฉ๋๋ค.
1๊ฐ์ ์ข์์
dimohy
5์ 7, 2021, 3:18์ค์
2
public void MainFunc(TKey key)
{
TestFunc(key);
}
๋์ ,
public void MainFunc(TKey[] key)
{
TestFunc(key);
}
ํ์
์ผ ํฉ๋๋ค.
1๊ฐ์ ์ข์์
๋น ๋ฅธ ๋ต๋ณ ๊ฐ์ฌํฉ๋๋ค dimohy๋.
ํ์ง๋ง ์๋ ์ฝ๋์ฒ๋ผ ๋ณ๊ฒฝํด๋ณด์๋ ๋์ผํ ํ์์ด ๋ฐ์ํฉ๋๋ค.
ํน์ ์ ๊ฐ ์ฐฉ๊ฐํ ๋ถ๋ถ์ด ์์๊น์?
public class Main
{
public class Main()
{
var testClass = new TestClass<SomeClass, object>();
testClass.MainFunc(new object[] { "str1", "str2" });
}
}
public class TestClass<TEntity, TKey>
where TEntity : class
{
public void MainFunc(TKey[] key)
{
TestFunc(key);
}
public void TestFunc(params object[] objectArray)
{
}
}
1๊ฐ์ ์ข์์
dimohy
5์ 7, 2021, 3:39์ค์
4
์๋๋๋ค. ์ ๊ฐ ํ
์คํธ ์ฝ๋๋ก ๊ฒ์ฆํ์ง ์์ ์๊ธด ๋ฌธ์ ์
๋๋ค.
TKey[] key ์ญ์ object[]๊ฐ ์๋ TKey[]์ด๊ณ object์ด๋ฏ๋ก ์ฒซ๋ฒ์งธ ์ธ์๋ก ๋ค์ด๊ฐ๋๊ฒ ๋ง์ต๋๋ค. ์ด๋ฅผ ํด๊ฒฐํ๋ ค๋ฉด object[] ๋ก ์บ์คํ
ํด์ผ ํ๋๋ฐ์,
Testfunc(key.Cast<object>().ToArray());
TestFunc๊ฐ object[] ๋์ TKey[]๋ฅผ ๋ฐ์ ์ ์๋ค๋ฉด ๊ตณ์ด ์บ์คํ
ํ ํ์๋ ์์ ๊ฒ ๊ฐ์ต๋๋ค.
public void TestFunc(params TKey[] objectArray)
{
}
2๊ฐ์ ์ข์์
์ดํด๋ฅผ ๋๊ธฐ์ํด TestFunc์ผ๋ก ํํํ์์ง๋ง ์ฌ์ค TestFunc๋ ์ธ๋ถ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ผ ํด๋น ํจ์ ์์ ์ ํ๋ค ๊ฒ ๊ฐ์ต๋๋ค.
key.Cast().ToArray() ๋ฅผ ์ฌ์ฉํ์ฌ์ผ ํ ๊ฒ ๊ฐ๋ค์.
(where โฆ, TKey: class๋ ํด๋น ํด๋์ค๋ฅผ ์ฌ์ฉํ๋ ๋ค๋ฅธ ๊ฐ์ฒด ๋๋ฌธ์ ์ฌ์ฉ์ด ๋ถ๊ฐ๋ฅํ๊ณ ์.)
์น์ ํ ๋ต๋ณ ์ ๋ง ๊ฐ์ฌ๋๋ฆฝ๋๋ค!
2๊ฐ์ ์ข์์